in reply to Re: XS: SvPVLV examples?
in thread XS: SvPVLV examples?

Right off the bat, that eliminates magic.

I disagree. If you are dealing with numeric data in its binary form, you will always have to do some pre and post processing of the binary values if you are to combine them with normal math opcodes. The pack and unpack in that unwieldy line I posted.

By using magic, the lvalue can appear as a normal perl variable with the appropriate numeric attributes without having to process pack/unpack templates and all the complexities they involve, or the unwieldyness of non-lvalue expressions. But I see you reached the same conclusion later.

Then substr magic is definitely not acceptable,

I knew that I would be using corrected versions of the substr magic set/get. To both correct the deficiencies, and simplify the logic.

I looked in perlxs quickly, and I didn't see anything about this.

The XS docs came up short in my searches for how to implement lvalue XS subs, but as I showed in my modifyied version of your sub above, just the addition of a lvalue prototype for the XS subs before use does work.

I found a reference to it from back in the 5.7.x days http://www.mail-archive.com/perl5-porters@perl.org/msg89367.html. I also found a reference to an XS keyword ATTRS, but this doesn't show up in the docs anywhere.

Sounds like you want your function to return numbers

The idea is that the subs will return a PVLV that will act like the appropriate form of IV/UV/NV etc. but without the need to create a full-blown SV for every offset. Preferably, the endianness of a particular value will be retained within the lvalue itself so that it doesn't need to be reasserted for every use.

BTW: Thanks for the to magic subs you posted elsewhere. One thought. It would be easier to follow if the posts were in a single contiguous thread rather than scattered around.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^3: XS: SvPVLV examples?
by ikegami (Patriarch) on Sep 25, 2009 at 19:50 UTC

    I disagree. If you are dealing with numeric data in its binary form, you will always have to do some pre and post processing of the binary values

    That has nothing to do with whether you use

    ++( $mmap->uint8(0x1234) );
    or the faster
    $mmap->preinc_uint8(0x1234);

    Now, using magic allows you to reuse existing code (like the ++ operator), so I thought you'd be willing to absorb the cost. That's why I proceeded.

    It would be easier to follow if the posts were in a single contiguous thread rather than scattered around.

    Then depth of the thread was causing problems. I took the fresh start from actually breaking out of the XY problem as a good point to restart. Your post actually forks that new branch.

      How about the even faster:++ u8( $mmap, 0x12346789abcd );

      or faster and simpler

      my $uchar = u8( $mmap, 0x12346789abcd ); ++$$uchar if cond; $$uchar *= 3;

      I've seen much deeper threads and never seen it as a problem.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Even then, I bet avoiding magic is still faster.
        my $byte = u8( $mmap, 0x12346789abcd ); ++$byte if cond; u8( $mmap, 0x12346789abcd, $byte * 3 );

        If you search PM, you'll find benchmarks between 4-arg and l-lvalue substr. I don't remember how much slower the latter is, but it's quite a bit slower.