D array expansion and non-deterministic re-allocation

Bartosz Milewski bartosz-nospam at relisoft.com
Wed Nov 18 10:21:49 PST 2009


Andrei Alexandrescu Wrote:

> Leandro Lucarella wrote:
> > Andrei Alexandrescu, el 17 de noviembre a las 18:45 me escribiste:
> >>> 3.  If you **really** care about performance, you should only append when you
> >>> don't know the length in advance.  If you know the length, you should always
> >>> pre-allocate.
> >> We will have collections and all those good things, but I don't see
> >> how the proposal follows from the feedback. My perception is that
> >> this is a group of people who use D. Bartosz' concern didn't cause
> >> quite a riot, so as far as I can tell there is no big issue at
> >> stake.
> > 
> > I didn't say anything (until now) because this was discussed already.
> > Dynamic arrays/slices appending is horribly broken (I know it's well
> > defined, and deterministic, but you are just condemned to make mistakes,
> > it just doesn't work as one would expect, even when you know how it works
> > you have to keep fighting your intuition all the time).
> 
> In which ways do you think arrays horribly broken? Same as Bartosz mentions?
> 
> One question is whether you actually have had bugs and problems coding, 
> or if arrays stopped you from getting work done.
> 

What Leandro points out is that using D arrays is not straightforward and may present a steep learning curve. In particular, even the beginner would be required to understand section 4.1.9 of TDPL (Expanding). For many people arrays' behavior might be counter-intuitive and a source of mistakes.

In fact, even after reading 4.1.9 I don't know what to expect in some cases. Here's an example:

int[] a = [0];
auto b = a;
a ~= 1;
b ~= 2;
What is a[1]?

Is this considered "stomping" and requiring a re-allocation?




More information about the Digitalmars-d mailing list