Recently in PDL I was reimplementing often-used Perl functions in XS for raw speed gainz. A problem I dealt with was effectively dispatching to another Perl method in some circumstances with the same Perl arguments, as efficiently as possible.

I'll leave out how it took a couple of days of experimenting to figure this out; that wasn't due to the dispatching technique being wrong, but a simple logic error a bit further up the function. If there's any value to mentioning that here, it's to check your assumptions (possibly with printf) when things don't go to expectations.

The obvious way to do this, according to https://perldoc.perl.org/perlcall, is to pass the G_NOARGS flag. However, that is (somewhat obscurely) documented as not being a good idea to use with perl_call_method. The technique I settled on, permalinked at https://github.com/PDLPorters/pdl/blob/cd5ffff1b85ff81effefc2085067e9d71801efd6/Basic/Core/Core.xs#L199-L220:

SP -= items; PUSHMARK(SP); SPAGAIN; /* these pass this set of ar +gs on */ int retvals = perl_call_method("new", G_SCALAR); SPAGAIN; if (retvals != 1) barf("new returned no values"); RETVAL = POPs;
For completeness, this is the full text of the function:
SV * topdl(klass, arg1, ...) SV *klass; SV *arg1; CODE: if (items > 2 || (!SvROK(arg1) && SvTYPE(arg1) < SVt_PVAV) || (SvROK(arg1) && SvTYPE(SvRV(arg1)) == SVt_PVAV) ) { SP -= items; PUSHMARK(SP); SPAGAIN; /* these pass this set of ar +gs on */ int retvals = perl_call_method("new", G_SCALAR); SPAGAIN; if (retvals != 1) barf("new returned no values"); RETVAL = POPs; } else if (SvROK(arg1) && SvOBJECT(SvRV(arg1))) { RETVAL = arg1; } else { barf("Can not convert a %s to a %s", sv_reftype(arg1, 1), SvPV_n +olen(klass)); } SvREFCNT_inc(RETVAL); OUTPUT: RETVAL
EDIT to explain the magic a bit more: SP is the local copy (which Perl does for efficiency when you repeatedly push arguments) of the global current "Perl stack pointer" (as I am calling it here): the address of ST(items-1) for the next function called, i.e. the top of the current stack frame. When you PUSHMARK an address, that will be ST(0) for the next Perl function that gets called. items for that next function will be the global "Perl stack pointer" minus that next function's ST(0) (plus 1). SPAGAIN copies the global "Perl stack pointer" into SP - I am carefully not saying "copies back", because of the faintly tricky stuff done here.

Breaking the code into individual statements, with comments for each:

SP -= items; /* make the local SP point at our ST(0) */ PUSHMARK(SP); /* make the next function's ST(0) be that */ SPAGAIN; /* reset our local SP to the global "top of current stack" */ int retvals = perl_call_method("new", G_SCALAR); /* call method so has + right stack frame */ SPAGAIN; /* set our local SP to the global "top of current stack" */ if (retvals != 1) barf("new returned no values"); /* use croak in non- +PDL code */ RETVAL = POPs; /* set the SV* to the return value */
You may think, as I have just realised in writing this, that the first 3 lines do unnecessary work, since PUSHMARK(SP - items) would valuably replace them. You would be right! And I have just done this. However, leaving this example as it is still seems valuable (and the generated machine code is likely to be extremely similar), so I am doing so.

In reply to XS: How to call another Perl function/method with same arguments by etj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.