http://www.perlmonks.org?node_id=928049


in reply to Re^4: XS: returning a 64-bit unsigned int?
in thread XS: returning a 64-bit unsigned int?

It ought to be possible to make a single determination at the top of a routine

That is how it's done. A single SvGETMAGIC if you read a scalar, no matter how many times you read it. A single SvSETMAGIC if you write to a scalar, no matter how many times you write to it. You don't want to call magic twice, and not just for performance reasons.

Replies are listed 'Best First'.
Re^6: XS: returning a 64-bit unsigned int?
by BrowserUk (Patriarch) on Sep 27, 2011 at 09:21 UTC
    That is how it's done. A single SvGETMAGIC if you read a scalar, no matter how many times you read it. A single SvSETMAGIC if you write to a scalar, no matter how many times you write to it.

    And if you're processing a real array containing a million real scalars, then that's 2 million redundant, time consuming function calls conditional tests.

    Addition: Were it possible to perform a single test on the array to detect magic, it might be worth doing to allow me to gracefully reject it. But as is, the only sensible option is to document that only real arrays of real scalars are acceptable input and fail noisily if I am given anything else.


    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.

      If they're redundant, don't do them. Generally speaking, though, they're not redundant. The outcome of each test is independent of the others. Magic can be tied to an element without being tied to the array, and the use of GET and SET magic is independent.

      $ perl -MDevel::Peek -E' sub TIESCALAR { bless \my $x; } sub FETCH { 3 } my @a = (6,7); tie $a[1], __PACKAGE__; say $a[0]; say $a[1]; Dump(\@a); ' 6 3 SV = IV(0x9a22324) at 0x9a22328 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9a3c2e8 SV = PVAV(0x9a23174) at 0x9a3c2e8 REFCNT = 2 FLAGS = (PADMY) ARRAY = 0x9a3af28 FILL = 1 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9a21764) at 0x9a21768 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 6 Elt No. 1 SV = PVMG(0x9a64d50) at 0x9a21898 REFCNT = 1 FLAGS = (GMG,SMG,RMG,pIOK,pPOK) IV = 3 NV = 0 PV = 0x9a370d0 "3"\0 CUR = 1 LEN = 12 MAGIC = 0x9a46460 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x9a22258 SV = IV(0x9a22254) at 0x9a22258 REFCNT = 1 FLAGS = (ROK) RV = 0x9a4f4a0
        Magic can be tied to an element without being tied to the array.

        I know. That's why it is so expensive to cater for.

        The fact that almost no one uses tied scalars for anything makes it all the worse.


        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.