Message 48958 - Python tracker (original) (raw)

Logged In: YES user_id=858364

It avoids generating constant objects with sizes above 20 (in a similar fashion as the bytecode peepholer), and checks whether the operand of unary minus is non-zero in order to avoid changing -0.0.

As for the bytecode peephole optimizer, this AST constant folder performs quite similar optimizations, but optimizes partially constant and/or and comparative expressions in addition. This patch should however not be seen as a replacement for the bytecode constant folder, but rather as a complement. An optimizing compiler typically contains many forms of constant folding in the different phases of compilation, since many later optimizations benefit from constant folding (warranting early constant folding), and some optimizations might emit code that benefit from constant folding again (warranting late constant folding). For an example of the former, consider the statement

if 1-1: some_code()

both passes are able to transform this into

if 0: some_code()

but since the AST constant folder is run before the dead code eliminator at <http://python.org/sf/1346214>, these two together are able to optimize the if statement away altogether.

Note that this patch probably won't apply cleanly anymore, since it was written three months ago and the AST code has undergone quite a few changes since then. But if there is interest in applying this patch, I'll gladly update it for the current trunk.