in reply to (Expert) Splicing a slice

I seriously doubt this is possible, even from XS.

Whilst you could set up two xpvav structures to point to the same SV* array and get two arrays where one is a slice of the other, the moment any modification was made to one of the arrays that changed its length, push,pop, splice etc., the perl library code for performing these operations would not maintain both xpvavs. At best, the modification to the modified array would not be reflected in the other. At worst, you'll get a segfault.

Eg.

AV xpvav [(@foo)]->[ARRAY]-------------------| [FILL ] = 4 | [MAX ] = 9 | [... ] | [ALLOC]-----------| | [ARLEN] v v SV*[ | | 1 | 2 | 3 | 4 | 5 | | | ] AV xpvav ^ ^ [(@bar)]->[ARRAY]-------------------| [FILL ] = 4 | [MAX ] = 9 | [... ] | [ALLOC]-----------| [ARLEN]

After splice @foo 1, 0, 'a' .. 'c';, if you were really lucky and there was enough contiguous freespace following the existing allocation to accomodate the extension to the array, then you might end up with something like this:

AV xpvav [(@foo)]->[ARRAY]-------------------| [FILL ] = 7 | [MAX ] = 13 | [... ] | [ALLOC]-----------| | [ARLEN] v v SV*[ | | 1 | a | b | c | 2 | 3 | 4 | 5 | | + | ] AV xpvav ^ ^ [(@bar)]->[ARRAY]-------------------| [FILL ] = 4 | [MAX ] = 9 | [... ] | [ALLOC]-----------| [ARLEN]

but now the FILL & MAX fields in the @BAR xpvav no longer reflect the reality of the memory to which they point because the library code is not going to maintain both structures. Any attempt to use @bar is at best going to result in erroneous results, and worst is likely to segfault.

And if there is not sufficient contiguous memory following the existing allocation when the modification is made, a completely new piece of memory will be allocated and the existing values from @foo will be copied to the new allocation, but again, the @bar xpvav will not be maintained and any correspondance between the two arrays will be lost.

There is also the problem of maintain the reference counts. If you take and hold a reference to one array, but the other's reference count falls to zero, then perl will be free to reclaim the space as it will not know about the other array's reference(s).


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.