On 12/22/20 5:12 PM, Rekel wrote:
According to the D slice article (https://dlang.org/articles/d-array-article.html), slices do not care where they start, only where they end, when checking whether expanding in place is permitable, or at least that is what I understood regarding it.

Now I'm unsure how to check this, I tried to a bit using the online editor and a bit of pointer usage which seemed to confirm my suspicion, but does this mean that taking a (small) slice at the end of a (possibly) very large dynamic array can lead to problematic behavior?

Problematic in what way? It will not stomp on data that is live. So I'd say it's the opposite of problematic.

You can check whether an append will reallocate or not by checking the capacity. If arr.capacity <= arr.length, then an append will reallocate.

For example;

int[] a = new int[10]; // Imagine this is very large
int[] b = a[$-1..$];   // Small slice at the end
b ~= 2;                // b extends, possibly in place
a ~= -1;               // a no longer can, so the entire array needs reallocating
(instead of be reallocating & a growing in place)

For sure a will need reallocating. The runtime cannot know that you will append with a, so it uses the space for b.

If you'd rather b reallocate, you can use the concatenation operator on it instead:

b = b ~ 2;

Or you can manage the array allocations yourself without using the runtime (see std.array.Appender).


Again I'm not very certain I fully understood how slices are implemented, but is this example, and the problem I imagine it leading to, valid?

Is the description you have valid? yes. Is it a problem with the implementation? I'd say no. If you change your expectations, you can avoid this situation easily.

-Steve

Reply via email to