Thread overview
Prevent slices from referencing old array after realocation?
May 27, 2015
Gr8Ape
May 27, 2015
Márcio Martins
May 27, 2015
I'm writing a program that handles lots of slices of larger
arrays. I need the data of each of them all together for one
operation, but otherwise it's more convenient to handle the
elements in small groups. I could just store a pointer to my
array, an offset, and a length but since I'm using D I'd much
prefer to use slices...

So I was looking through the documentation on slices and found
out that if the array has to realocate then slices pointing to
that array will keep on pointing to that block of memory, rather
than the new location. Given that I'll be working with arrays
that can vary wildly in size and that giving all of them the
maximum capacity so they never realocate would be extremely
inefficent, I need a workaround.

So I'm wondering if there's a way to tell all the slices of an
underlying array to now point to the new underlying array upon
reallocation? Or would I have to iterate through and reassign
every slice?
May 27, 2015
On 5/27/15 1:55 AM, Gr8Ape wrote:
> I'm writing a program that handles lots of slices of larger
> arrays. I need the data of each of them all together for one
> operation, but otherwise it's more convenient to handle the
> elements in small groups. I could just store a pointer to my
> array, an offset, and a length but since I'm using D I'd much
> prefer to use slices...
>
> So I was looking through the documentation on slices and found
> out that if the array has to realocate then slices pointing to
> that array will keep on pointing to that block of memory, rather
> than the new location. Given that I'll be working with arrays
> that can vary wildly in size and that giving all of them the
> maximum capacity so they never realocate would be extremely
> inefficent, I need a workaround.
>
> So I'm wondering if there's a way to tell all the slices of an
> underlying array to now point to the new underlying array upon
> reallocation? Or would I have to iterate through and reassign
> every slice?

Slices can't do this, because they have no idea about each other. So when you append to one slice, you can't have it update all other relevant slices.

However, you could create a type that does this.

-Steve
May 27, 2015
On Wednesday, 27 May 2015 at 07:55:29 UTC, Gr8Ape wrote:
>

You can keep an index and a count instead of a slice.
If you know your data well, you can exploit this knowledge, and this has the potential to save you many bytes per "slice" when dealing with large data sets.
Depending on your access patterns, it also has the potential to be fairly more cpu-efficient.