On Tue, 24 Nov 2009 15:29:22 -0500, Bartosz Milewski
<[email protected]> wrote:
Andrei Alexandrescu Wrote:
Array may include a field
bool sliceExtracted;
that is set to true whenever you take a slice from the array and set to
false whenever the array's data is reallocated. The array's
documentation could mention that ~= is amortized constant if there are
no intervening slicing operations.
In other words, an array may include a field "isUnique" that is set to
true after every re-allocation and turned off whenever the compiler
can't prove uniqueness--e.g., when a slice is taken. This "dynamic
uniqueness" is different from "static uniqueness". The latter could be
made part of the type system and checked at compile time, but the former
might be just enough for the purpose of optimization.
What is important is that it would clear the semantics of array
expansion--it would always predictably break the connection to its
slices, if any. Note also that if slices are just two pointers, they
will never dangle because the GC will guarantee that the original is not
recycled as long as somebody is pointing into it.
If this could be implemented, I'd feel much better about D arrays.
I'll add this to my opinion on Andrei's idea: If Array is a suplementary
type in addition to slices (that is, appending to slices is still
possible, and do not necessarily reallocate on appending), then I agree
this is along the right lines for an appending array type used to build
arrays as efficiently as possible.
-Steve