Oops! Good catch. I wonder how perl's not bailing out complaining e.g.

Can't find label CODE(0x81678a0) at sub.pl line 13.

and seems to be doing the right thing instead. The corrected code yields

Rate refcall named goto refcall 708190/s -- -1% -10% named 717414/s 1% -- -9% goto 790676/s 12% 10% --
but your previous reply (Re: named sub, subref and magic goto speed) explains that well: the goto &$ref essentially saves the building of one stack frame; that makes sense, thank you.

<update>

Uhm, no, goto &sub does create a new stack frame. From what I read in pp_ctl.c without knowing all those macros, I guess it's working like this:

A new frame is created, the old frame is unwound (sort of return), the old frame inspected (whether inside a sub, an eval, a loop and so on), then @_ pushed onto the stack again and the new frame is entered (sort of call).

It seems that the magic goto &function works along the semantics of

my $ref = *function{'CODE'}; # get a reference goto $ref; # magic goto to that reference

which explains why the goto &$coderef is more expensive - it does an extra deref and ref step:

my $ref = \&{$coderef}; # get a reference (\&{&$ref} without +the call) goto $ref; # magic goto to that reference

whereas whithout the & we have just

goto $ref; # we have a code reference, just use +that

Roughly. I mean, explaining what's going on in C speaking perl is sort of... well ;-)

The relevant part of a diff of the output of two runs of the same code (foo.pl), once with goto $ref and once with goto &$ref (with -Dtls) shows what's going on:

--- g_ref.Dtls +++ g_amp.Dtls @@ -69,7 +69,13 @@ => (foo.pl:5) nextstate => +(foo.pl:5) pushmark + => * (foo.pl:5) padsv($ref) + => * \CV(__ANON__) +(foo.pl:5) rv2cv + => * CV(__ANON__) +(foo.pl:5) refgen => \CV(__ANON__) (foo.pl:5) goto (foo.pl:5) (Found sub #1)

<update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^2: named sub, subref and magic goto speed by shmem
in thread named sub, subref and magic goto speed by shmem

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.