perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Optimise away empty if{}, elsif{}. and else {} blocks

Open richardleach opened this issue 6 months ago • 3 comments

Note: this is an (extended) replacement for https://github.com/Perl/perl5/pull/22745

These commits:

  • Change Perl_op_scope so that a bare OP_STUB is not wrapped in an OP_ENTER/OP_LEAVE pair.
  • Change Perl_rpeep to remove the that OP_STUB when 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.

richardleach avatar Jun 11 '25 22:06 richardleach

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.

bulk88 avatar Jun 12 '25 22:06 bulk88

Also, more seriously, would this optimization degrade or break NYTProf? ?

bulk88 avatar Jun 12 '25 22:06 bulk88

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.

richardleach avatar Jun 12 '25 22:06 richardleach

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,}

richardleach avatar Jul 07 '25 16:07 richardleach

@bulk88 or committers, any further comments on this that should block its merging?

richardleach avatar Jul 13 '25 17:07 richardleach

(Force-pushed to fix merge conflicts.)

richardleach avatar Jul 14 '25 22:07 richardleach

@tonycoz - you reviewed the previous incarnation, do you have bandwidth to take a look at this one please?

richardleach avatar Jul 25 '25 23:07 richardleach

Will this PR break https://metacpan.org/pod/Enbugger ?

Enbugger has been broken for a very long time.

tonycoz avatar Jul 27 '25 23:07 tonycoz