Dtnamic array memory allocation
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jan 8 05:22:43 PST 2010
On Thu, 07 Jan 2010 23:32:21 -0500, Rob Adelberg <adelmetz at mindspring.com>
wrote:
> Does the dynamic array pre-allocate any memory?
No. It allocates on request. Appending does pre-allocate more memory
then asked, the logic being if you are repeatedly appending, you want to
optimize the allocations.
>
> I'm curious if the ~= on an array would be time sensitive. Would it make
> sense
> to allocate memory myself and then reset the length?
If by time sensitive, you mean it is a performance killer? It is to some
degree :) There is a patch in the works for D2 that will make it more
efficient, but if you require the best efficiency, I'd suggest using an
array builder object like bearophile alluded to. That way, your appends
don't need to look up GC info.
>
> class A
> {
> uint [] array;
> :
> this()
> {
> array = new uint [10];
> array.length = 0;
> }
> }
This is a common trick, but after the patch it will be incorrect on D2
(the syntax will compile, but your preallocation will be wasted). There
will be a new function to replace this.
-Steve
More information about the Digitalmars-d-learn
mailing list