Pop quiz [memory usage]

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Jun 6 10:33:29 PDT 2009


Sean Kelly wrote:
> Jarrett Billingsley wrote:
>> On Sat, Jun 6, 2009 at 8:03 AM, Vladimir
>> Panteleev<thecybershadow at gmail.com> wrote:
>>> // Works for DMD1/Phobos, DMD1/Tango and DMD2/Phobos
>>> version(Tango) import tango.io.Console;
>>> else           import std.stdio;
>>>
>>> struct S
>>> {
>>>        ubyte[40_000] data;
>>> }
>>>
>>> void main()
>>> {
>>>        S[] a;
>>>        a ~= S();
>>>
>>>        // QUESTION: How much memory will this program consume upon 
>>> reaching
>>> this point?
>>>        version(Tango) Cin.copyln();
>>>        else           readln();
>>> }
>>>
>>
>> There seems to be something wrong with the newCapacity function that
>> _d_arrayappendcT calls.  From an element size of 20000 (I halved it
>> just to make the allocation faster) and an array length of 1, it
>> somehow calculates the new size to be 266686600.  Hm.  That seems a
>> bit off.
>>
>> It seems this line:
>>
>> long mult = 100 + (1000L * size) / log2plus1(newcap);
>>
>> is to blame.  I don't think large value types were taken into account
>> here.  The resulting multiplier is 1,333,433, which is hilariously
>> large.
> 
> I've been debating for a while whether newCapacity shoulds exist at all. 
>  The GC already tends to leave free space at the end of blocks, so why 
> should the runtime second-guess it?  Particularly in D2 where append 
> operations on arrays are probably less common as a result of string 
> being invariant.  I'll fix newCapacity and run some tests, depending on 
> their result I may disable it entirely.

I think that's a great point.

Andrei



More information about the Digitalmars-d mailing list