points by fasterik 4 days ago | 0 comments
I think this is missing the forest for the trees. The potential savings of one extra size_t per allocation are miniscule compared to the overhead of using malloc() in the first place, with a potential syscall on every allocation. If you care about waste, you should ask for those pages from the OS directly, once at init, and use arenas.
[–] 0c3ca83 4 days ago
There's no syscall on every allocation. With many allocators, the common case is popping a node off a linked list.
[–] fasterik 4 days ago
That's why I put the word "potential" in there. Every allocation potentially needs to go to the OS for more pages.
[–] 0c3ca83 4 days ago
And every memory access potentially enters the kernel via a page fault.
[–] cowlevel 3 days ago
Any given CPU instruction potentially isn't in cache.
And yet, they can't all not be in cache. As soon as the cache is accessed for one instruction, a bunch more are pulled in too.
Any given vector.push call could reallocate the vector, but it's amortized O(1).
[–] OskarS 4 days ago
[–] fasterik 4 days ago
[–] OskarS 4 days ago
BTW, when malloc() takes 10,000 cycles, you've either hit some weird edge case in an allocator or (much more likely) malloc() has run out pages it can use to hand over to the application and has to ask the kernel for more memory. I measured malloc() for a recent change in one of our products, and in my test the median time was less than 100ns. malloc() is usually very fast. But with a better API, it could be faster. Multiplied by every memory allocation on every computer in the world (more or less) for the last half-century, it's becomes a very big number.