View source code
Display the source code in rt/lifetime.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function rt.lifetime.newCapacity

Given an array of length size that needs to be expanded to newlength, compute a new capacity.

ulong newCapacity (
  ulong newlength,
  ulong size
);

Better version by Dave Fladebo: This uses an inverse logorithmic algorithm to pre-allocate a bit more space for larger arrays. - Arrays smaller than PAGESIZE bytes are left as-is, so for the most common cases, memory allocation is 1 to 1. The small overhead added doesn't affect small array perf. (it's virtually the same as current). - Larger arrays have some space pre-allocated. - As the arrays grow, the relative pre-allocated space shrinks. - The logorithmic algorithm allocates relatively more space for mid-size arrays, making it very fast for medium arrays (for mid-to-large arrays, this turns out to be quite a bit faster than the equivalent realloc() code in C, on Linux at least. Small arrays are just as fast as GCC). - Perhaps most importantly, overall memory usage and stress on the GC is decreased significantly for demanding environments.

Parameters

NameDescription
newlength new .length
size old .length

Returns

new capacity for array

Authors

Walter Bright, Sean Kelly, Steven Schveighoffer

License

Distributed under the Boost Software License 1.0. (See accompanying file LICENSE)