News for hackers. Stuff that matters.
Re:The lost malloc() by fasterik on Wednesday November 04, 2026 @08:56AM (#50365566)
I think this is missing the forest for the trees. The potential savings of one extra size_t per allocation is 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.
Re:The lost malloc() by OskarS on Wednesday November 04, 2026 @09:32AM (#50366052)
If you think you can convince the entire computer industry to stop using malloc(), I wish you the best of luck. In actuality, your computer likely calls malloc() somewhere thousands of times per second in low use, probably millions of times during intensive use. Wouldn't it be nice if all those calls were just a bit less resource intensive?
Re:The lost malloc() by fasterik on Wednesday November 04, 2026 @09:45AM (#50366246)
I'm curious, what do you expect the performance improvement to be, in terms of CPU cycles per allocation? malloc() costs on the order of 10,000 CPU cycles on the slow path.
Re:The lost malloc() by OskarS on Wednesday November 04, 2026 @10:08AM (#50366633)
I don't know what the exact impact would be, but it's certainly not zero, and multiplied by every malloc, it adds up. My point is mostly that the API for malloc()/free() is designed in such a way to both limit the kind of implementations you can make for it, and make most implementations a tiny bit slower than they need to be.

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.

Re:The lost malloc() by 0c3ca83 on Wednesday November 04, 2026 @09:44AM (#50366230)
There's no syscall on every allocation.
Re:The lost malloc() by fasterik on Wednesday November 04, 2026 @09:51AM (#50366319)
That's why I put the word "potential" in there. Every allocation potentially needs to go to the OS for more pages.
Re:The lost malloc() by 0c3ca83 on Wednesday November 04, 2026 @10:34AM (#50367055)
And every memory access potentially enters the kernel via a page fault.
Re:The lost malloc() by cowlevel on Wednesday November 04, 2026 @08:46PM (#50374026)
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).