[Python-Dev] Expression optimizations

Raymond Hettinger python at rcn.com
Tue Feb 10 20:15:44 CET 2009


----- Original Message ----- 
From: "Cesare Di Mauro" <cesare.dimauro at a-tono.com>
To: "Python-Dev" <python-dev at python.org>
Sent: Tuesday, February 10, 2009 8:24 AM
Subject: [Python-Dev] Expression optimizations


> In peephole.c I noticed some expression optimizations:
>
> /* not a is b -->  a is not b
>    not a in b -->  a not in b
>    not a is not b -->  a is b
>    not a not in b -->  a in b
> */
>
> So, it seems that an operation can be changed to another one which is logically equivalent.
>
> Could it be applyable to other operations as well? So, if I wrote:
>
>  c = not(a < b)
>
> the compiler and/or peephole optimizer can generate bytecodes instructions which, instead, execute the following operation:
>
>  c = a >= b
>
> Is it right?

We've only done conservative transformations that do not change which magic methods get called.  The is / isnot transformations are 
invisible to the programmer and always semantically neutral.  Your proposed transformation is changes which methods get called and 
makes assumptions that the usual relationships between comparison operators holds (but it might not given rich comparisons, for 
example, sets use the comparison operators for subset/superset tests).


Raymond 



More information about the Python-Dev mailing list