in reply to named sub, subref and magic goto speed

For refcall vs. named, you aren't seeing a significant difference.

WRT the stack frame, refcall and named have to build a new stack frame and put the contents of @_ on the stack; that's heavy lifting.

  • Comment on Re: named sub, subref and magic goto speed

Replies are listed 'Best First'.
Re^2: named sub, subref and magic goto speed
by Limbic~Region (Chancellor) on Dec 14, 2006 at 13:41 UTC
    ysth,
    How does this differ from goto ⊂? I ask because of this.

    Cheers - L~R

      Apparently I was mistaken about the stack frame (unless it's changed since Larry wrote that). But the "heavy lifting" I meant was loading @_ onto the stack. For purposes of comparison:
      $ perl -MO=Concise,-exec,named,refcall,goto1,goto2 -we'my $ref = \&bar +; sub bar {} sub named { bar(@_) } sub refcall { $ref->(@_) } sub goto1 { goto &$re +f } sub goto2 { goto $ref }' main::named: 1 <;> nextstate(main 3 -e:2) v 2 <0> pushmark s 3 <$> gv(*_) s 4 <1> rv2av[t1] lKM/1 5 <$> gv(*bar) s 6 <1> entersub[t2] KS/TARG,1 7 <1> leavesub[1 ref] K/REFC,1 main::refcall: 8 <;> nextstate(main 4 -e:2) v 9 <0> pushmark s a <$> gv(*_) s b <1> rv2av[t2] lKM/1 c <0> padsv[$ref:FAKE] s d <1> entersub[t3] KS/TARG,1 e <1> leavesub[1 ref] K/REFC,1 main::goto1: f <;> nextstate(main 5 -e:2) v g <0> pushmark sRM h <0> padsv[$ref:FAKE] s i <1> rv2cv[t2] lKRM/33 j <1> refgen sK/1 k <1> goto sKS/1 l <1> leavesub[1 ref] K/REFC,1 main::goto2: m <;> nextstate(main 6 -e:2) v n <0> padsv[$ref:FAKE] s o <1> goto sKS/1 p <1> leavesub[1 ref] K/REFC,1 -e syntax OK
      Miscellaneous points:

      You can see that goto $ref bypasses an unnecessary deref/refgen that goto &$ref does.

      refcall and named have to load @_ onto the stack; the gotos don't.

      refcall and named differ only by one operation: gv vs. padsv. gv and padsv are both trivial ops (except when padsv has flags set that it doesn't here) that just push a value on the stack, so I wouldn't expect any significant difference between them.

      I'm not sure what you're asking, but goto &$ref is no faster than foo(), which is in line with the linked node.

      use strict; use warnings; use Benchmark qw( cmpthese ); sub foo { @_ } my $ref = \&foo; cmpthese(-2, { refcall => sub { @_=(1); $ref->(@_) }, named => sub { @_=(1); foo(@_) }, g_scalar => sub { @_=(1); goto $ref }, g_amp => sub { @_=(1); goto &$ref }, });

      outputs

      Rate named refcall g_amp g_scalar named 811837/s -- -1% -3% -26% refcall 820126/s 1% -- -2% -25% g_amp 834174/s 3% 2% -- -24% g_scalar 1099304/s 35% 34% 32% --
        ikegami,

        ysth said:

        WRT the stack frame, refcall and named have to build a new stack frame and put the contents of @_ on the stack; that's heavy lifting.

        To me, that implied this wasn't the case for goto $ref; which is really you say is goto &$ref;. I was asking for clarification on that point.

        Update: See <del> and <ins> for minor wording modification.

        Cheers - L~R