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.

Replies are listed 'Best First'.
Re: XS: How to call another Perl function/method with same arguments
by harangzsolt33 (Deacon) on Jun 10, 2024 at 03:58 UTC
    This is mostly Chinese to me. But I hope that one day maybe I will understand some of what you said. Lol

    For example, what is "SV"? I see that a lot.

      Some entrails of Perl (in this case: the API) can be found in perldoc perlguts. The section on Datatypes explains
      • SV: Scalar Value
      • AV: Array Value
      • HV: Hash Value
      Of course, it isn't actually Chinese, but C ;-)