Re:On C Linked Lists (Profiling and Optimizing)
by tptacek on Sunday December 12, 2010 @03:02PM
(#1997848)
This is a great comment and I guess it's good advice, but doubling vectors have never caused a problem for me, I profile, and I've had them dealing with data sets like "each side of every TCP connection traversing 1/8th of an entire tier 1 ISP backbone".
I'll steal the Python trick, because it's neat, but I don't think worrying about your resize function is a good reason to procrastinate moving away from linked lists, which are mostly evil.
Don't do this.
Doubling the size of your allocation whenever you expand can easily lead to large amounts of wasted storage in common scenarios. "Yes," you may say. "But memory is cheap." Which it is at the small scale, but when you're allocating 1GB to store a 512MB array you have a problem.
A vastly better approach is to use some form of adaptive rescaling which avoids excessive overcommits.
Here, for example, is the code (and explanatory comment) Python uses to determine the new allocation size for an expanding list:
Note that you will also want to avoid using the same calculation for expanding and contracting the allocation size, to avoid thrashing allocations when the list size varies around the pivot point. (i.e., add one element, triggering an expansion; remove the element, triggering a contraction; repeat.)