in reply to Help me improve my XS.

The following works even with the commented out lines and doesn't appear to leak at all.

There's no leak since you don't create mortals. I think it would also clear mortals returns by the called function, but maybe print returns nothing in void context?

Why does it crash in a Benchmark?

PUTBACK should come before the call, although it's not even required when there are no args.

The documentation uses G_DISCARD instead of G_VOID.

You change GvSV(PL_defgv).

Do any of those matter? dunno.

The code you posted doesn't crash for me using v5.12.2 built for i686-linux.

How can it be improved?

SAVE_DEFSV; appears to be the means to save $_, and DEFSV_set(sv); appears to be the means to set it.

Does that work if there's a my $_ in scope of the callback? (Is that even possible?)

I don't think it's safe to assume the SV still has a PV slot after the callback.

You could accept strings in either formats instead of suffering from The Unicode Bug.

Are there any documentation or examples of using LvTARG macros to do the aliasing?

LvTARG is used by some types magic (e.g. lvalue substr) as they see fit. While you could use magic to effectively do aliasing, that wouldn't be as efficient as actually aliasing.

It's pretty straightforward. Check out function pp_substr and the functions related to substr in mg.c

Is there any documentation of the MULTICALL macros used by List::Util::reduce()?

Multicall is documented in "LIGHTWEIGHT CALLBACKS" in perlcall.

How can I apply a (&$) prototype to strIter()?

Using the PROTOTYPE XS directive.

Update: Mentioned SAVE_DEFSV and DEFSV_set.

Replies are listed 'Best First'.
Re^2: Help me improve my XS.
by ikegami (Patriarch) on Nov 24, 2010 at 01:08 UTC

    I didn't explain properly about the lack of leak.

    There would be no leak even if you did use mortals. There's already a ENTER+SAVETMPS and FREETMPS+LEAVE around the call to the XS function.

    You'd use those or G_DISCARD if you want to free the mortals created by your callback sooner (e.g. after every call to call_sv instead of when strIter returns).