Array allocations [was: Re: What does C++...]
Don
nospam at nospam.com
Sat Jul 30 11:43:51 PDT 2011
bearophile wrote:
> Peter Alexander:
>
>> I also posted my thoughts, although I think I was a bit more negative
>> towards D than you were :-)
>
> I think your answer is among the best of that thread.
> Regarding this you have written:
>
>> int[3] a = [1, 2, 3]; // in D, this allocates then copies
>> int a[3] = {1, 2, 3}; // in C++, this doesn't allocate
>>
>> Apparently, to avoid the allocation in D, you must do:
>>
>> static const int[3] staticA = [1, 2, 3]; // in data segment
>> int[3] a = staticA; // non-allocating copy
>>
>> These little 'behind your back' allocations are good examples of my previous two points.
>
> Memory allocations caused by this, inside an inner loop, have given me performance troubles.
> I presume this problem has solutions. Isn't it possible to add a small optimization to DMD to avoid this problem?
>
> Bye,
> bearophile
Yeah, it's not fundamental, and not even very complicated. The current
implementation was a quick hack to provide the functionality, that
hasn't been replaced with a proper implementation yet. All that's
required to fix it is a bit of code in e2ir.c.
More information about the Digitalmars-d
mailing list