On Tue, 11 Aug 2020 12:18:19 +0200
demerphq <demerphq@gmail.com> wrote:
> On Tue, 11 Aug 2020 at 12:13, demerphq <demerphq@gmail.com> wrote:
> >
> > On Mon, 10 Aug 2020 at 15:15, Felipe Gasper
> > <felipe@felipegasper.com> wrote:
> > >
> > >
> > >
> > > > On Aug 10, 2020, at 7:40 AM, Dave Mitchell <davem@iabyn.com>
> > > > wrote:
> > > >
> > > > On Wed, Aug 05, 2020 at 11:29:12PM +0200, Tomasz Konojacki
> > > > wrote:
> > > >> On Wed, 5 Aug 2020 10:54:50 -0400
> > > >> Felipe Gasper <felipe@felipegasper.com> wrote:
> > > >>
> > > >>> This interface appears to be undocumented, but per IRC
> > > >>> discussion it? useful and widely used.
> > > >>>
> > > >>> Would it be appropriate at this point to document? I? happy
> > > >>> to contribute if so.
> > > >>>
> > > >>> Thank you!
> > > >>>
> > > >>> -FG
> > > >>
> > > >> My guess is it wasn't documented because making it a public
> > > >> API would make changing the underlying datastructure of AVs to
> > > >> anything else than a plain C array impossible (e.g. to some
> > > >> kind of sparse array or perhaps an indexable skiplist).
> > > >>
> > > >> I think that ship has sailed decades ago because, as you said,
> > > >> it's being widely used, and even if it wasn't, I think it's
> > > >> unlikely we would accept the performance trade-offs of the
> > > >> other datastructures.
> > > >
> > > > Note that there is an important difference between AvARRAY(av)
> > > > and AvARRAY(av)[n]. The former can be API without the latter
> > > > being. In particular, a few years ago we changed unused
> > > > elements of an array from being 'AvARRAY(av)[n] = &PL_sv_undef'
> > > > to 'AvARRAY(av)[n] = NULL'. This caused a certain very vocal XS
> > > > author to declare that once again we had deliberately sabotaged
> > > > perl.
> > > >
> > > > I personally would leave AvARRAY not part of the API unless we
> > > > are going to start guaranteeing all aspects of its behaviour.
> > >
> > > Hm.
> > >
> > > My use case is to implement splice(@array, 0, 1) in XS. After
> > > initially thinking I’d have to iterate through the AV, it was
> > > pointed out in IRC that I could simply Move() the AvARRAY
> > > contents. For now that’s what I’m doing, but if AvARRAY is to be
> > > omitted from the API by design then I may need to revisit.
> >
> > This is not what Perl would do. You should not move anything when
> > you remove items from the front of an array. You increment the array
> > pointer, and then fiddle with a flag so that perl knows you have
> > hidden items at the start of the array. I believe it stores the
> > number of elements hidden in the IV part of the struct, but i
> > havent looked closely.
>
> More generically the only time you should move data in an AV with a
> splice/remove operation is when you are removing items from the
> middle, and then you have no choice but to move items (and should
> probably take care to move the least items).
But that was the entire point of the original question - in effect, an
API for doing splice() operations.
--
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk |
https://metacpan.org/author/PEVANS http://www.leonerd.org.uk/ |
https://www.tindie.com/stores/leonerd/