[Python-Dev] AST optimizer implemented in Python

Meador Inge meadori at gmail.com
Sun Aug 12 23:05:15 CEST 2012

On Sat, Aug 11, 2012 at 1:30 PM, Victor Stinner
<victor.stinner at gmail.com> wrote:

> Hi,
> 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.
> https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py

Very cool.

> To test its peephole optimizations (by checking manually its final
> bytecode), I wrote a patch for Python to disable Python internal
> peephole optimizer (on bytecode):
> https://bitbucket.org/haypo/misc/src/tip/python/compile_disable_peephole.patch
> --
> There is BytecodeAssembler [1], but it seems to be specialized on
> bytecode. There are (at least?) 3 different issues to implement an AST
> optimizer, but in C, not in Python:
> http://bugs.python.org/issue1346238
> http://bugs.python.org/issue10399
> http://bugs.python.org/issue11549

I read through the issues a while back and each is interesting in its own
right.  However, each is a specific implementation that is somewhat general,
but geared towards one optimization (folding, inlining, etc...).

ISTM, that we need to step back a bit and define a what an AST
optimizer for Python should look like (or even if it really makes any
sense at all).  I imagine having some facilities to manage and add new
passes would be useful, for instance.  I think this work probably merits a
PEP (considering we essentially have four competing implementations for
AST optimization now).

This is an interesting project and I would happily volunteer to help flesh
out the details of a prototype and working on a PEP.

# Meador

More information about the Python-Dev mailing list