Memory leak with dynamic array
Don
nospam at nospam.com
Tue Apr 13 13:27:36 PDT 2010
Joseph Wakeling wrote:
> Steven Schveighoffer wrote:
>> Assuming you may ask questions about this, it's not an exact description
>> of what happens. The append code and GC are smarter about it than this,
>> I just wanted to explain it in an easy-to-understand way :) The real
>> code uses algorithms to determine optimal grow sizes and can extend into
>> consecutive blocks if possible.
>
> I realised that while running some of the code that bearophile suggested
> to me, because if you look at 'capacity' in a non-pre-reserved D array
> that's appended to 5 million times, its capacity is only a little over 5
> million -- compared to a C++ vector which is the nearest power of 2
> (2^23, over 8 million).
>
> The GC overhead means that in this case actual memory usage is a bit
> higher than C++, but on a larger scale this could surely make a big
> difference.
>
> In the case of the piece of code I'm working on I don't think the
> pre-reserve is really so important as far as performance goes -- the
> append speed is a bit of a bugger, but the main issue is probably to do
> with speed of copying and iterating across arrays.
>
> For example, I suspect that the D array's,
>
> x[] = 0;
> y[] = z[];
>
> is not as efficient as a C++ vector's
>
> x.assign(x.size(),0);
> y.assign(z.begin(),z.end());
The D code compiles directly to a memset and memcpy, which are
intrinsics. There's no way C++ could beat it.
More information about the Digitalmars-d-learn
mailing list