in reply to Re^2: Why is the execution order of subexpressions undefined? (victim)
in thread Why is the execution order of subexpressions undefined?

It would be sad if Perl 6 were not free to optimize
 my $x = $z * ( $y + 1 ) + $w / ( $y + 1 );

Why would defining execution order affect that optimisation?

That just common subexpression elimination--clearly defined by the parens. The only sane defined ordering would be to execute the contents of parens first?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?
  • Comment on Re^3: Why is the execution order of subexpressions undefined? (victim)
  • Download Code

Replies are listed 'Best First'.
Re^4: Why is the execution order of subexpressions undefined? (sane?)
by tye (Sage) on Apr 12, 2005 at 05:42 UTC

    I'd be insane to calculate $z first?

    So which is the defined order that you find so easy to come up with? You can only have one.

    - tye        

      Sub expressions are evaluated before the expressions they are a part of. Order of evaluation within a subexpression is in precedence order with ties broken left to right.

      In the expression you gave, both ( $y + 1 ) are subexpressions and would be evaluated before the overall expression, left to right. The fact that they are the same subexpression gives rise to the possible optimisation.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco.
      Rule 1 has a caveat! -- Who broke the cabal?

        So does that mean $z is calculated before $y+1 or not? In what order does $w get calculated?

        $w= $z; $x= ( $a + $w ) * ( $y + 1 ) + ( $a + $z ) / ( $y + 1 );

        The optimizer notices that $w and $z must be equal and so we need only add $a to their shared value once. In what order does this single operation happen relative to the rest of the calculations? If I change the first assignment, surely the order of operations for the second assignment doesn't change. What is the obvious answer to this?

        - tye