Optimise away empty if{}, elsif{}. and else {} blocks
Note: this is an (extended) replacement for https://github.com/Perl/perl5/pull/22745
These commits:
- Change
Perl_op_scopeso that a bareOP_STUBis not wrapped in anOP_ENTER/OP_LEAVEpair. - Change
Perl_rpeepto remove the thatOP_STUBwhen it is safe to do so (not in scalar context).
Tests and B::Deparse support are included.
For example, given my $x; if ($x) {print 1} else {}, the relevant piece of the optree looked like this before:
4 <;> nextstate(main 2 -e:1) v:{ ->5
- <1> null vK/1 ->a
6 <|> cond_expr(other->7) vK/1 ->b
5 <0> padsv[$x:1,8] s ->6
- <@> scope vK ->-
- <;> ex-nextstate(main 4 -e:1) v ->7
9 <@> print vK ->a
7 <0> pushmark s ->8
8 <$> const[IV 1] s ->9
d <@> leave vK ->a
b <0> enter v ->c
c <0> stub vP ->d
With these commits, it looks like:
4 <;> nextstate(main 2 -e:1) v:{ ->5
- <1> null vK/1 ->a
6 <|> cond_expr(other->7) vK*/1 ->a
5 <0> padsv[$x:1,8] s ->6
- <@> scope vK ->-
- <;> ex-nextstate(main 4 -e:1) v ->7
9 <@> print vK ->a
7 <0> pushmark s ->8
8 <$> const[IV 1] s ->9
- I'm not sure if this set of changes requires a perldelta entry.
troll argument: will this "break" or change flaky PP single stepping to extra crispy flaky PP single stepping?
I don't have the time to compile this PR and do a deep dive on how Komodo's red dots and yellow arrow behave B4/AF when Im rapid tapping F11. The yellow arrow has never been perfect for me, 5.10 thru blead, its good enough for me, its not perfect. The yellow arrow has always gotten weird around constant folded if elsif {} braces and loop open loop close {} braces since I wrote PP hello world.
Update:
BBQ glazed step rolls argument: Will this PR break https://metacpan.org/pod/Enbugger ?
@bulk88 last used https://metacpan.org/pod/Enbugger in 2016. After 2016 he reverse engineered Komodo's PP DBR's PP DBR injector code, along with rev engineering and synthesizing cmd line -D in PP code. Its really easy, just keep a strategic copy paste in a .txt you add to whatever strict.pm your perl of the day is executing, and obviously its #ed off 99% of the time inside strict.pm, but no .t or perl.exe in the world will escape my little trick. Shell vars %PERL5OPT% and %PERL5DB% are useless IMO. There are entire frameworks on CPAN, Where the root class/root .pm in a BEGIN{}, think of huge frameworks like Alien:: or Moose::, I forgot the exact names of what CPAN base class modules did this to me. But %PERL5OPT% and %PERL5DB% are utterly useless, since the .t or PolymorphicMetaProgramming::Base::Test.pm probes $ENV{PERL5OPT} and $ENV{PERL5DB} and then deletes the 2 env vars, then launches fresh_perl_like($0);
Even better is CPAN authors who have a secure coding policy of if (keys %ENV) {local(%ENV); exit(system($^X, $0));}. I've had to fix this 2 or 4 in the past atleast once involving CPAN modules. Hence time to edit strict.pm by hand and pretend $ENV{PERL5OPT} and $ENV{PERL5DB} and -D dont exist in Perl.
Also, more seriously, would this optimization degrade or break NYTProf? ?
Also, more seriously, would this optimization degrade or break NYTProf? ?
I'll try to check at the weekend.
I haven't used Komodo in yonks.
Also, more seriously, would this optimization degrade or break NYTProf? ?
All NYTProf install tests passed for me and manually stepping through a test file didn't bring up any problems. Empty blocks with a comment showed up like:
0.000606,10000,0.000000,if ($x) {
0.000000,0,0.000000,# do nothing
0.000000,0,0.000000,} else {
0.000000,0,0.000000,$count++;
0.000000,0,0.000000,}
and completely empty blocks like this:
0.000945,10000,0.000000,if ($x) {
0.000000,0,0.000000,} else {
0.000000,0,0.000000,$count++;
0.000000,0,0.000000,}
@bulk88 or committers, any further comments on this that should block its merging?
(Force-pushed to fix merge conflicts.)
@tonycoz - you reviewed the previous incarnation, do you have bandwidth to take a look at this one please?
Will this PR break https://metacpan.org/pod/Enbugger ?
Enbugger has been broken for a very long time.