Optimise out PUSHMARK/RETURN if return is the last statement in a sub.
This makes:
sub baz { return $cat; }
Behave like:
sub baz { $cat; }
Which is notably faster.
Unpatched:
./perl -Ilib/ ~/stuff/bench.pl
Benchmark: timing
40000000 iterations of normal, ret...
normal: 3 wallclock secs ( 1.60 usr + 0.01 sys = 1.61 CPU) @
24844720.50/s (n=
40000000)
ret: 3 wallclock secs ( 2.08 usr + 0.00 sys = 2.08 CPU) @
19230769.23/s (n=
40000000)
Patched:
./perl -Ilib ~/stuff/bench.pl
Benchmark: timing
40000000 iterations of aret, normal...
normal: 2 wallclock secs ( 1.72 usr + 0.00 sys = 1.72 CPU) @
23255813.95/s (n=
40000000)
ret: 2 wallclock secs ( 1.72 usr + 0.00 sys = 1.72 CPU) @
23255813.95/s (n=
40000000)
The difference in OP trees can be seen here:
Unpatched:
$ perl -MO=Concise,baz -e 'sub baz { return $cat }'
main::baz:
5 <1> leavesub[1 ref] K/REFC,1 ->(end)
- <@> lineseq KP ->5
1 <;> nextstate(main 1 -e:1) v ->2
4 <@> return K ->5
2 <0> pushmark s ->3
- <1> ex-rv2sv sK/1 ->4
3 <#> gvsv[*cat] s ->4
-e syntax OK
Patched:
$ ./perl -Ilib -MO=Concise,baz -e 'sub baz { return $cat }'
main::baz:
3 <1> leavesub[1 ref] K/REFC,1 ->(end)
- <@> lineseq KP ->3
1 <;> nextstate(main 1 -e:1) v ->2
- <@> return K ->-
- <0> pushmark s ->2
- <1> ex-rv2sv sK/1 ->-
2 <$> gvsv(*cat) s ->3
-e syntax OK
(Includes some modifications from Steffen)