[Python-Dev] AST optimizer implemented in Python

Chris Angelico rosuav at gmail.com
Sun Aug 12 01:22:17 CEST 2012

On Sun, Aug 12, 2012 at 4:30 AM, Victor Stinner
<victor.stinner at gmail.com> wrote:
> I started to implement an AST optimizer in Python. It's easy to create
> a new AST tree, so I'm surprised that I didn't find any existing
> project.

Very nice idea!

> Other idea to improve this optimizer:
>  - move invariant out of loops. Example: "x=[]; for i in range(10):
> x.append(i)" => "x=[]; x_append=x.append; for i in range(10):
> x_append(i)". Require to infer the type of variables.

But this is risky. It's theoretically possible for x.append to replace
itself. Sure it may not be a normal or common thing to do, but it's
possible. And yet it could be pretty advantageous for most programs.

Perhaps the best way is to hide potentially-risky optimizations behind
command-line options? The default mode could be to do every change
that's guaranteed not to affect execution, and everything else is an
extra (like French, music, and washing).


More information about the Python-Dev mailing list