The great slice debate -- should slices be separated from arrays?

Bartosz Milewski bartosz-nospam at relisoft.com
Tue Nov 24 12:29:22 PST 2009


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.



More information about the Digitalmars-d mailing list