Hacker News
Why is the first C++ (m)allocation always 72 KB?
kgeist
|next
[-]
Reminds of .NET preallocating OutOfMemoryException and similar:
https://github.com/dotnet/runtime/blob/f1b1b935ee5bafd149ae0...
pjmlp
|next
|previous
[-]
surajrmal
|root
|parent
|next
[-]
zabzonk
|root
|parent
|next
|previous
[-]
pjmlp
|root
|parent
[-]
anonymousiam
|next
|previous
[-]
jebarker
|next
|previous
[-]
ozgrakkurt
|root
|parent
|next
[-]
"You can't do it, just use a library.". "Just use this library, everyone uses it.". "Even google uses this library, do you think you are better." etc.
To add another example to this, you will read that memcpy is super mega optimized on libc and you shouldn't do it yourself etc. etc. etc.
But if you just check clickhouse [1] as an example. They implemented it, it is pretty basic and they say it works well in the comments of the code.
Also you can check musl libc code etc. and it is fairly simple.
People still would argue that you used some intrinsic so it isn't portable or you just benchmarked on one case so it won't work well overall.
Well you CAN benchmark as wide as you want inside your project and have a different memcpy code per project. This kind of thing isn't as bad as people make it out to be in my opinion.
Ofc memcpy is just an example here and it applies similarly to memory allocation, io etc.
As a negative note, imo this is one of the major reasons why most software is super crappy now. Everything uses some library -> those libraries change all the time -> more breaking -> more maintenance. Similar chain happens in terms of performance because the person that wrote that library probably doesn't even know how I am using the library.
This is also why people have endless arguments about what library/tool to use while they can be learning more and more things every day.
[1] https://github.com/ClickHouse/ClickHouse/blob/master/base/gl...
syncsynchalt
|root
|parent
|previous
[-]
In ELF objects (i.e. on Linux) this is usually done with the "Weak" symbol binding. This is an optional flag for symbols in ELF format that let you override a symbol by providing a competing non-weak symbol, which the linker will prefer when there is a conflict. https://en.wikipedia.org/wiki/Weak_symbol
You can see the list of Weak symbols by looking for a 'W' in the output of `nm` on linux hosts.
Joker_vD
|next
|previous
[-]
joelsiks
|root
|parent
[-]
throwaway2037
|next
|previous
[-]
Also, I cannot find his email address anywhere (to ask him to share it on GitHub).
Am I missing something?
joelsiks
|root
|parent
|next
[-]
nly
|root
|parent
|previous
[-]
https://catonmat.net/simple-ld-preload-tutorial-part-two
There's actually a better way to hook GNUs malloc:
https://www.man7.org/linux/man-pages/man3/malloc_hook.3.html
This is better because you can disable the hook inside the callback, and therefore use malloc within your malloc hook (no recursion)
But you can't use this mechanism before main()
Joker_vD
|root
|parent
[-]
The use of these hook functions is not safe in multithreaded
programs, and they are now deprecated. From glibc 2.24 onwards,
the __malloc_initialize_hook variable has been removed from the
API, and from glibc 2.34 onwards, all the hook variables have been
removed from the API. Programmers should instead preempt calls to
the relevant functions by defining and exporting malloc(), free(),
realloc(), and calloc().
nly
|root
|parent
[-]
The global variable approach was very useful and pretty low overhead.
znpy
|next
|previous
[-]
Reminds me of Perl's $^M: https://perldoc.perl.org/variables/$%5EM
In Perl you can "hand-manage" that. This line would allocate a 64K buffer for use in an emergency:
$^M = 'a' x (1 << 16);