Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: XS: EXTEND/mPUSHi

by BrowserUk (Patriarch)
on Sep 26, 2011 at 16:30 UTC ( [id://927895]=note: print w/replies, xml ) Need Help??


in reply to Re: XS: EXTEND/mPUSHi
in thread XS: EXTEND/mPUSHi

Thanks.

Yes. I know it can be done using the ludicrously verbose, Camel_Case_And_Underscores inline stack macros. I was trying to understand why I can't use the neater and more concise XS macros from Inline C. I've found I can reduce the requirements to:

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'monkeys', CLEAN_AFTER_BUILD => 0; void rnd64( int n ) { Inline_Stack_Vars; static unsigned __int64 y = 88172645463325252i64; EXTEND( SP, n ); while( n-- ) { y ^= y << 13; y ^= y >> 7; y ^= y << 17; mPUSHu( y ); } Inline_Stack_Done; return; }

But looking at the C produced by the above, it looks like there is a path through the generated wrapper function that avoids both the XSRETURN_EMPTY and the PUTBACK, thus returning whatever has been pushed:

XS(XS_main_rnd64); /* prototype to pass -Wmissing-prototypes */ XS(XS_main_rnd64) { #ifdef dVAR dVAR; dXSARGS; #else dXSARGS; #endif if (items != 1) croak_xs_usage(cv, "n"); PERL_UNUSED_VAR(ax); /* -Wall */ SP -= items; { int n = (int)SvIV(ST(0)); #line 46 "monkeys.xs" I32* temp; #line 117 "monkeys.c" #line 48 "monkeys.xs" temp = PL_markstack_ptr++; rnd64(n); if (PL_markstack_ptr != temp) { /* truly void, because dXSARGS not invoked */ PL_markstack_ptr = temp; XSRETURN_EMPTY; /* return empty stack */ } /* must have used dXSARGS; list context implied */ return; /* assume stack size is correct */ ### Her +e #line 128 "monkeys.c" PUTBACK; return; } }

But I'm obviously missing something in the OP code that would cause (or allow) it to follow that path?


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.

Replies are listed 'Best First'.
Re^3: XS: EXTEND/mPUSHi
by syphilis (Archbishop) on Sep 26, 2011 at 22:51 UTC
    I know it can be done using the ludicrously verbose, Camel_Case_And_Underscores inline stack macros

    The camel case can be avoided - use either INLINE_STACK_VARS or inline_stack_vars instead of Inline_Stack_Vars (etc, etc ...).
    But Inline currently offers no alternative to the verbosity or the underscores - which is one of a number of reasons that I, too, often prefer to use the XS equivalents.

    If you ever want to check on what they are, these macros can be found in Inline/C.pm in the "Generate the INLINE.h file" section, or in any Inline-generated Inline.h that you can lay your hands on.

    I think these macros are mostly useful for beginners in that they provide a mantra that gets most jobs done - and despite their verbosity, are easier to remember than the corresponding list of XS symbols. (After a while away from doing any Inline::C or XS stuff, I'm flat out remembering whether I want to start my Inline::C script with "dSP" or "dXSARGS", but I can always remember "Inline_Stack_Vars" :-)

    Cheers,
    Rob

      dXSARGS is the most comprehensive.

      Or you could just look at the error message you get and add the appropriate declaration (spdSP;, axdAX;, markdMARK;, etc).

      Inline/C.pm uses sp = mark;, when it should probably use SP = MARK;, and I'd not even sure if that's officially allowed.

        Inline/C.pm uses sp = mark;, when it should probably use SP = MARK;, and I'd not even sure if that's officially allowed

        In pp.h I can see:
        #define SP sp #define MARK mark
        So I'm guessing that case is not going to be critical.

        But I don't know whether the assignment is officially allowed. The perl source itself makes that assignment in a number of places - but that doesn't necessarily give that usage the green light.

        How would you adjust the stack pointer if you wanted to ? Is altering it something that you would prefer to avoid ?

        Cheers,
        Rob
      these macros can be found in Inline/C.pm

      The problem is not in finding their definitions, it is understanding what they do, when they must be used and when not etc.

      Their definitions are all in terms of other (XS) macros, and unwinding those is much harder. And once you have unwound them to the actual code that gets executed, it is generally a horrible mess of nested macro expansions calling a bunch of undocumented apis -- often repetitively -- and manipulating another bunch of undocumented global variables.

      Trying to work out what calls are actually need when, rather than just cargo-culting someone else who cargo-culted someone else who ... is very difficult. Weirdness like the completely unused & redundant PUTBACK; return; sequence generated by the Inline::C wrapper functions just compounds matters.

      I think these macros are mostly useful for beginners in that they provide a mantra that gets most jobs done - and despite their verbosity, are easier to remember than the corresponding list of XS symbols.

      Do you really find verbosity more memorable than conciseness? I don't. I realise that you inherited those definitions, but I think that it is a fallacy to believe they are more memorable than the XS_equivalents.

      With Ike's help above, I've reduced my fast random generator to:

      void rnd64( int n ) { dXSARGS; static unsigned __int64 y = 88172645463325252i64; EXTEND( SP, n ); while( n-- ) { y ^= y << 13; y ^= y >> 7; y ^= y << 17; mPUSHu( y ); } PUTBACK; return; }

      Which appears to work well. It doesn't appear to leak any memory after producing over 12 billion 64-bit rands (in batches of 1 million) in an hour , but that still leaves me with the question of whether I should be resetting the stack to account for the single input parameter, or whether the EXTEND() takes care for that for me?

      And it is this aspect of using Inline::C or XS that leaves me cold. The reason for dipping into the quagmire is to achieve speed unobtainable in Perl, but working out what bits of the templated examples are actually required and when is (it seems) only possible by suck-it-and-see.

      I also wonder if I shouldn't be passing in a reference to the an array and populating it directly, rather than assigning the returned stack to it?

      Equally, rather than creating a whole new bunch of SVs to hold my rands in the target array, couldn't I just modify their IVs in place? Would that be more efficient?


      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.
        but that still leaves me with the question of whether I should be resetting the stack to account for the single input parameter, or whether the EXTEND() takes care for that for me

        No - as it stands, the first value that gets returned should be the supplied argument - at least that's what I'm finding. If you want to avoid that, then you need to reset the stack pointer or take some alternative measure.
        Ike questions the validity of doing sp = mark though it's something that has been in the Inline test suite for the last few years, and has not produced any problems with the cpan-testers. Is there a better way of resetting the stack pointer ?
        I think that, in this instance you could also just sp--; (update: or, more generally, sp -= items) before you EXTEND.

        I can't actually find any code of mine that resets the stack pointer - I usually take a different approach such as just assigning to ST(0), ST(1), etc.

        I also wonder if I shouldn't be passing in a reference to the an array and populating it directly, rather than assigning the returned stack to it?

        I've benchmarked that for some huge arrays in the past ... and not detected any advantage in passing by reference (with Inline::C).

        Cheers,
        Rob

        It doesn't appear to leak any memory

        Indeed, there's no leak.

        Perl's stack isn't refcounted, so that means you can't decrement the refcount of a scalar you place on the stack even if you don't keep a reference to it.

        What you do is place the scalar on a list of variables whose refcount needs to be decremented when they are removed from the stack. Variables on this list are called mortals.

        (This way of doing things is a source of many bugs, but that's the box you have to play in.)

        The "m" in "mPUSHi" stands for mortal. The created scalar will be made mortal.

        mPUSHi(4);
        boils down to
        *(++SP) = sv_2mortal(newSViv(4));

        but that still leaves me with the question of whether I should be resetting the stack to account for the single input parameter

        Check the first value being returned. It's always your argument (n).

        or whether the EXTEND() takes care for that for me?

        As documented EXTEND will make sure there is n spaces available. That's it. It doesn't remove anything already there.

        In your case, you end up with at least n+1 spaces on the the stack total, one of which is used for the argument.

        Weirdness like the completely unused & redundant PUTBACK; return; sequence generated by the Inline::C wrapper functions

        It's actually added by the XS to C converter, xsubpp. Inline::C doesn't put there. In fact, Inline::C specifically doesn't want it, but doesn't have a way of telling xsubpp not to call it.

        Inline::C requires that you call PUTBACK (explicitly or via XSRETURN) because it can't do it for you because it doesn't have access to your sub's SP to "put it back".

        Real XS doesn't require that you call PUTBACK because xsubpp actually creates the sub, so it has access to the sub's SP.

        PS — C doesn't require the use of return; when the return type is void.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://927895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found