Pop quiz [memory usage]

Fawzi Mohamed fmohamed at mac.com
Sun Jun 7 02:51:19 PDT 2009


On 2009-06-07 10:47:47 +0200, Christian Kamm 
<kamm-incasoftware at removethis.de> said:

> bearophile wrote:
> 
>>> The following code even crashes LDC during the compilation, I'll ask in
>>> the LDC channel:<
>> 
>> The good ChristianK has already added it:
>> http://www.dsource.org/projects/ldc/ticket/320
> 
> And thankfully Frits van Bommel has already fixed it: it consumes about 40kb
> of heap memory at runtime now.
> 
> This seems to be because we don't use the _d_arrayappend functions at all
> but emit a _d_arraysetlength instead. What's the memory growing behavior of
> that function? Should this be considered a bug?

I would say that setlength should not allocate extra space, because one 
should trust that the user knows his needs, whereas if the array grows 
while appending then it is reasonable to add extra space because the 
user is probably appending without really knowing/caring about array 
resizes.
So it is reasonable (and probably good) to try to second guess him, and 
add extra space.
This way repeatedly appending to that array has a good performance.

Thus yes I would say that it should be considered a bug (that will 
degrade repeated appending performance).

The solution is to use a better newCapacity function, instead of the 
flawed one.

Fawzi




More information about the Digitalmars-d mailing list