Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

op_scope/newCONDOP: Optimise away empty else blocks in OP_COND_EXPR #22745

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from

Conversation

richardleach
Copy link
Contributor

This commit comprises two main changes:

  • Not wrapping a bare OP_STUB in an enter/leave pair (Perl_op_scope)
  • Checking for a bare OP_STUB in the falseop argument when building an OP_COND_EXPR and then freeing it, rather than adding it as a sibling to the first and trueop branches.

The main benefits of this are:

  • lower memory usage due to unnecessary OP removal
  • faster execution due to unnecessary OPs not being executed

There are also some small changes to Deparse.pm and an additional Deparse.t test.


  • This set of changes requires a perldelta entry, but we are close enough to a point release that I'll add it separately.

This commit comprises two main changes:
* Not wrapping a bare OP_STUB in an enter/leave pair (Perl_op_scope)
* Checking for a bare OP_STUB in the `falseop` argument when building
  an OP_COND_EXPR and then freeing it, rather than adding it as a
  sibling to the `first` and `trueop` branches.

The main benefits of this are:
* lower memory usage due to unnecessary OP removal
* faster execution due to unnecessary OPs not being executed

There are also some small changes to Deparse.pm and an additional
Deparse.t test.
@richardleach
Copy link
Contributor Author

As a quick example, my $x; if ($x) { say 1 } else { }:
Before:

a  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 2 -e:1) v:%,us,{,fea=15 ->3
3     <0> padsv[$x:2,9] vM/LVINTRO ->4
4     <;> nextstate(main 3 -e:1) v:%,us,{,fea=15 ->5
-     <1> null vK/1 ->a
6        <|> cond_expr(other->7) vK/1 ->b
5           <0> padsv[$x:2,9] s ->6
-           <@> scope vK ->-
-              <;> ex-nextstate(main 5 -e:1) v:%,us,fea=15 ->7
9              <@> say 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

Now:

a  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter v ->2
2     <;> nextstate(main 8 -e:1) v:%,us,{,fea=15 ->3
3     <0> padsv[$x:8,15] vM/LVINTRO ->4
4     <;> nextstate(main 9 -e:1) v:%,us,{,fea=15 ->5
-     <1> null vK/1 ->a
6        <|> cond_expr(other->7) vK/1 ->a
5           <0> padsv[$x:8,15] s ->6
-           <@> scope vK ->-
-              <;> ex-nextstate(main 11 -e:1) v:%,us,fea=15 ->7
9              <@> say vK ->a
7                 <0> pushmark s ->8
8                 <$> const(IV 1) s ->9

@richardleach
Copy link
Contributor Author

The op.c changes to omit an empty if-true are also straightforward, but Deparse looks like being more work. This is why there isn't a single PR to cover both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant