As a follow up to Efficient walk/iterate along a string, I started playing with an inline C callback iterator. The following works even with the commented out lines and doesn't appear to leak at all.

However, when I tried to run it as part of a benchmark, it crashes.

So, ignoring the absence of error checking, my questions are:

#! perl -sw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'strIter', CLEAN_AFTER_BUILD => 0; void strIter( SV *code, SV *string ) { dSP; STRLEN i ; char *p = SvPVX( string ); SV *c; // ENTER; // SAVETMPS; c = newSV( 1 ); SvCUR( c ) = 1; SvLEN( c ) = 2; SvPVX( c ) = p; SvPOK_only( c ); GvSV(PL_defgv) = c; for( i=0; i < SvCUR( string ); ++i ) { PUSHMARK(SP); call_sv( code, G_NOARGS |G_VOID ); PUTBACK; SvPVX( c ) = ++p; SvPOK_only( c ); } // FREETMPS; // LEAVE; GvSV(PL_defgv) = string; } END_C use strict; use Time::HiRes qw[ time ]; use Benchmark qw[ cmpthese ]; our $string = 'abcdefghijklmnopqrstuvwyxABCDEFGHIJKLMNOPQRSTUVWXY' x 1 +00; for (1 .. 100 ) { strIter sub{ print; }, $string; }

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.
RIP an inspiration; A true Folk's Guy

In reply to Help me improve my XS. by BrowserUk

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.