Possible change to array runtime?

Timon Gehr timon.gehr at gmx.ch
Sun Mar 16 03:58:40 PDT 2014


On 03/14/2014 06:38 PM, Steven Schveighoffer wrote:
>
> I imagine that adding this 'feature' of setting length = 0 would help,
> but maybe just adding a new, but similar symbol for length that means
> "Do what D1 length would have done" would be less controversial for
> adding to druntime.  Example strawman:
>
> arr.slength = 0; // effectively the same as arr.length = 0;
> arr.assumeSafeAppend();
>
> It would do the same thing, but the idea is it would work for extension
> too -- if arr points at the beginning of the block, and slength *grows*
> into the block, it would work the same as D1 as well -- adjusting the
> "used" length and not reallocating.
>
> Essentially, you would have to s/.length/.slength, and everything would
> work. Of course, length is not a keyword, so there may be other cases
> where length is used (read property for instance) where slength would
> not necessarily have to be used.
>
> However, one thing we cannot fix is:
>
> arr = arr[0..$-1];
> arr ~= x;
>
> This would reallocate due to stomp prevention, and I can't fix that.

auto sslice(T)(T[] arr, size_t s, size_t e){
     auto r=arr[s..e];
     r.assumeSafeAppend();
     return r;
}

arr = arr.sslice(0, $-1);
arr ~= x;

> Do you have any cases like this, or do you always use .length?

I think just replacing all usages of features that have changed 
behaviour with library implementations that simulate the old behaviour 
would be quite easy.


More information about the Digitalmars-d mailing list