in reply to String Concatenation Performance

Per my reading of the opcodes, you'll spend a lot of time looking for something that's not there. There are two extra ops in the interpolation case, but they're optimized away.

$ perl -MO=Concise my $s = 'foo'; my $string = "${s}bar"; c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(PV "foo") s ->4 4 <0> padsv[$s:1,3] sRM*/LVINTRO ->5 6 <;> nextstate(main 2 -:2) v ->7 b <2> sassign vKS/2 ->c - <1> ex-stringify sK/1 ->a - <0> ex-pushmark s ->7 9 <2> concat[t3] sK/2 ->a 7 <0> padsv[$s:1,3] s ->8 8 <$> const(PV "bar") s ->9 a <0> padsv[$string:2,3] sRM*/LVINTRO ->b - syntax OK $ perl -MO=Concise my $s = 'foo'; my $string = $s . 'bar'; c <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(PV "foo") s ->4 4 <0> padsv[$s:1,3] sRM*/LVINTRO ->5 6 <;> nextstate(main 2 -:2) v ->7 b <2> sassign vKS/2 ->c 9 <2> concat[t3] sK/2 ->a 7 <0> padsv[$s:1,3] s ->8 8 <$> const(PV "bar") s ->9 a <0> padsv[$string:2,3] sRM*/LVINTRO ->b - syntax OK

Replies are listed 'Best First'.
Re: Re: String Concatenation Performance
by pbeckingham (Parson) on Mar 20, 2004 at 17:56 UTC

    Thank you - this is excellent. You have confirmed my suspicion that my tests were not good. Now I need to find a test that tricks the optimizer. Given that the ex-stringify sK/1 ->a op was removed, I guess using constant strings is not the way to test.

    I always understood that 'abc' was better than "abc", because of the interpolation, but it seems that the optimizer sees through this, given the constant strings, so all I am doing is being kinder to the optimizer. Premature optimization.

    The following now uses a string read from <>, and therefore not a constant:

    % perl -MO=Concise my $s=<>; my $t="$s"; b <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -:1) v ->3 - <1> null vKS/2 ->6 3 <0> padsv[$s:1,3] sRM*/LVINTRO ->4 5 <1> readline[t3] sKS/1 ->6 4 <#> gv[*ARGV] s ->5 6 <;> nextstate(main 2 -:2) v ->7 a <2> sassign vKS/2 ->b 8 <@> stringify[t5] sK/1 ->9 - <0> ex-pushmark s ->7 7 <0> padsv[$s:1,3] s ->8 9 <0> padsv[$t:2,3] sRM*/LVINTRO ->a
    I'll recreate tests based on this. Thank you all.

      The following now uses a string read from <>, and therefore not a constant:
      Your assumptions about what happens why are incorrect.
      $ perl -MO=Concise -e'my $s=""; my $t="$s";' b <@> leave&#91;$s:1,3] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(PV "") s ->4 4 <0> padsv[$s:1,3] sRM*/LVINTRO ->5 6 <;> nextstate(main 2 -e:1) v ->7 a <2> sassign vKS/2 ->b 8 <@> stringify[t3] sK/1 ->9 - <0> ex-pushmark s ->7 7 <0> padsv[$s:1,3] s ->8 9 <0> padsv[$t:2,3] sRM*/LVINTRO ->a -e syntax OK
      The "stringify" op is still active.

      Makeshifts last the longest.