develooper Front page | perl.perl5.porters | Postings from August 2008

unless(...) terser than if(!...)

Thread Next
From:
Nicholas Clark
Date:
August 28, 2008 14:45
Subject:
unless(...) terser than if(!...)
Message ID:
20080828214520.GS75181@plum.flirble.org
If I write this:

$ ./perl -Ilib -MO=Concise -e 'print if !$a'
9  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
-     <1> null vK/1 ->9
5        <|> and(other->6) vK/1 ->9
4           <1> not sK/1 ->5
-              <1> ex-rv2sv sK/1 ->4
3                 <#> gvsv[*a] s ->4
8           <@> print vK ->9
6              <0> pushmark s ->7
-              <1> ex-rv2sv sK/1 ->8
7                 <#> gvsv[*_] s ->8
-e syntax OK

it uses one more op than the semantically equivalent:

$ ./perl -Ilib -MO=Concise -e 'print unless $a'
8  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
-     <1> null vK/1 ->8
4        <|> or(other->5) vK/1 ->8
-           <1> ex-rv2sv sK/1 ->4
3              <#> gvsv[*a] s ->4
7           <@> print vK ->8
5              <0> pushmark s ->6
-              <1> ex-rv2sv sK/1 ->7
6                 <#> gvsv[*_] s ->7
-e syntax OK


Couldn't we optimise the former to the latter fairly easily?

[I'm just not sure of the right place to do it. It strikes me that there
ought to be somewhere when the if() is being built that could spot that
the topmost op of the condition tree is not, and simply remove the not
and swap the two sides of the LOGOP. But I'm really not familiar with how
the optree generation works, so I might be completely wrong.]

Nicholas Clark

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About