4 ms·
malloc() has incomplete information. It doesn't know that this allocation of 0 bytes is soon going to balloon to a few GB in a future realloc(), so if it starts
by geocar 7y ago
malloc() has incomplete information. It doesn't know that this allocation of 0 bytes is soon going to balloon to a few GB in a future realloc(), so if it starts in brk, it'll mmap later and memcpy the data over. The good news is that the caller knows this.
By saying "I want a sizeof(Cons) object" versus "I want a buffer that's going to get big and used for a long time" versus "I want a buffer that'll get big but go away right after" means you get to avoid these unnecessary operations. That's important because they really add up when dealing with lots of data. That's how replacing a complicated and intelligent malloc() which has to rely on heuristics to decide which of the "different paths" to follow can lose big to a simpler strategy.