Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by tye (Sage)
on Apr 12, 2005 at 05:12 UTC ( [id://446846]=note: print w/replies, xml ) Need Help??


in reply to Re: Why is the execution order of subexpressions undefined?
in thread Why is the execution order of subexpressions undefined?

I think you've fallen victim to the temptation that I posted about. I don't believe that Perl defines the order of evaluation of expressions based on the precedence table. That determines what the results will be but need not restrict Perl to arriving at the proscribed value in the exact order that you appear to have defined based on how Perl has so far been implemented.

It would be sad if Perl 6 were not free to optimize

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

By noting that $y + 1 is used twice and therefore could be computed beforehand and the result just used twice rather than computing it twice. But if you tie Perl's hand by saying that execution order is defined for such things, then Perl 6 will have no choice but to not optimize expressions, thwarting part of what was advertised as a big motivation for Parrot: the ability to optimize bytecode.

- tye        

Replies are listed 'Best First'.
Re^3: Why is the execution order of subexpressions undefined? (victim)
by Zaxo (Archbishop) on Apr 12, 2005 at 05:25 UTC

    Under an "as-if" rule, a bytecode optimizer need not obey the precedence rules, but the initial interpretation of your source must. Precedence and associativity are there to ensure that the expression you write is the one that gets calculated.

    After Compline,
    Zaxo

      We are talking order. The order that the calculations get performed need not be as you specified (based solely on precedence and associativity). Even if + might be redefined by the programmer such that x+y != y+x, computing x+y does not require that x be computed before y.

      - tye        

Re^3: Why is the execution order of subexpressions undefined? (victim)
by BrowserUk (Patriarch) on Apr 12, 2005 at 05:30 UTC
    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?

      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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://446846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-26 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found