Cleaned up C++

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 23 15:11:56 PDT 2015


On Thursday, 23 April 2015 at 18:37:47 UTC, Walter Bright wrote:
> On 4/23/2015 1:10 AM, bearophile wrote:
>> Walter Bright:
>>
>>> On 4/22/2015 2:58 PM, bearophile wrote:
>>>> D is less stack-friendly than Ada (and probably Rust too),
>>>
>>> ??
>>
>> In Ada standard library you have safe fixed-size 
>> stack-allocated associative
>> arrays. In D you can't even allocate safely a 
>> dynamically-sized 1D array on the
>> stack, and forget about doing it for 2D. Enough said.
>
> I used to use alloca() here and there, but eventually removed 
> it all. The trouble is, there are three array sizes:
>
>    a) 0
>    b) 1
>    c) arbitrarily large
>
> Dynamic stack allocation works for none of them. What does work 
> is a fixed size stack allocation with failover to using 
> malloc/free, which is what Phobos' scopebuffer does. It's 
> analogous to the "small string optimization".
>
> I don't agree with your assessment at all.

For arbitrary large, you can always do something like :

Item* itemPtr = (arbitrarylarge < thresold)
   ? alloca(arbitrarylarge)
   : GC.alloc(arbitrarylarge);

One extra check compared to a heap allocation is not going to 
make things terrible, and it is likely to be very predictible 
anyway (most arbitrarylarge size are actually small in practice).

The compiler could even do it at optimizer level.


More information about the Digitalmars-d mailing list