Simple question about how the optimizer works

James J. Besemer jb at cascade-sys.com
Thu May 9 11:52:28 EDT 2002


François Pinard wrote:

> Python does not really optimise the code it generates.  Some brave souls
> (Skip in particular) did flurries of tries in that direction, and kind of
> demonstrated that byte-code optimisations might not be as rewarding as we
> could have thought it would be.
>
> In the particular case above, CSE may not be applied, because the optimiser,
> if it existed, could not assume that `f' has no side-effect.  Suppose:
>
>     def f(x):
>         global y
>         y = y + 1
>         return x + y
>
> then `y' should be incremented four times, not one.

Some compilers have global optimizers that can recognize cases where
sufficiently simple functions do not have side-effects and then do common
sub-expression elimination even on function calls.  However, this is rare
because it's hard to do and because the opportunity to perform that particular
optimization is infrequent.

I'd expect there'd be more to gain in Python simply looking for patterns such as

    for i in xrange(N): ...

    for item in list: ...

    list comprehensions

and transforming them into a form where the loop is folded into lower level
runtime routines rather than executed long hand by byte-codes.

Regards

--jb


--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com







More information about the Python-list mailing list