2 ms·
If I'm reading this right, their array resizing was linear rather than geometric -- definitely a radical difference from the usual implementations! That is, eac
by pjscott 11y ago
If I'm reading this right, their array resizing was linear rather than geometric -- definitely a radical difference from the usual implementations! That is, each time they called realloc, they added a constant amount of capacity instead of doing the usual thing, which is multiplying it by some constant (e.g. doubling it). Consider what happens in the most naive implementation, where realloc calls malloc+memcpy+free, taking time proportional to the size of the buffer to be copied: appending n times to such an array would take O(n^2) time, rather than O(n) as you'd get from a linked list or from an array that grows geometrically.
Of course they can get away with it on most systems, because most memory allocators don't have that kind of naive implementation. They may do a geometric growth policy automatically; they may switch to grabbing memory from the OS's virtual memory system for allocations above a certain size; they can do all sorts of things that have the convenient side-effect of making the constant-growth algorithm here more efficient. But it shouldn't come as any great surprise that things can get weird on some systems with different memory management, when they've taken such an unusual approach to their array resizing.