D array expansion and non-deterministic re-allocation

Michel Fortin michel.fortin at michelf.com
Sun Nov 22 19:49:33 PST 2009


On 2009-11-22 19:58:37 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> It's very simple, really. Appending to an array never results in 
> overwriting elements of an existing array. Appending may terminate a 
> sharing relationship. This is simple, easy to understand, and requires 
> no understanding of the optimization paraphernalia, which I hope to be 
> able to improve.

I think it's the "may terminate" part Bartosz (and some others) don't 
like, since it's hard to predict and may cause bugs that appear in an 
hard to understand pattern when misused.

I'm not exactly sure where to stand on this issue, but I might have a 
solution that looks like a compromise: use the MRU only for arrays of 
immutable elements, like strings. Since it's not possible to modify 
immutable data, it doesn't change the semantics if two slices are 
aliasing each other or not[^1]. Other arrays could get the more 
predictable behavior of always reallocating, but an optimizer could 
still avoid that when static analysis can prove that only one slice 
points to the data.

[^1]: It changes things somewhat if you take the address of elements 
and start comparing them, but I find it reasonable that taking address 
of things could reveal implementation details. It could change things 
if the element type has a postblit function, or a destructor, since you 
will avoid some copying, but I think that's fine.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list