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.
I used to think this was totally wrong, but over time I've come
to see your point.
The ideal for me would be a dynamically sized stack array (C99
style, not alloca) with a compile-time maximum size. Then you
wrap that in a library type that decides/defines what to do when
the size is exceeded (e.g. move to GC heap a la scopeBuffer), but
in practice it's not a big win over just stack allocating the
maximum size all the time (again, like scopeBuffer).