Victor Stinner - FAT Python: a new static optimizer for Python 3.6
The Python language is hard to optimize. Let's see how guards checked
at runtime allows to implement new optimizations without breaking the
Python semantic.
-----
(Almost) everything in Python is mutable which makes Python a language
very difficult to optimize. Most optimizations rely on assumptions,
for example that builtin functions are not replaced. Optimizing Python
requires a trigger to disable optimization when an assumption is no
more true. FAT Python exactly does that with guards checked at
runtime. For example, an optimization relying on the builtin len
function is disabled when the function is replaced.
Guards allows to implement various optimizations. Examples: loop
unrolling (duplicate the loop body), constant folding (propagates
constants), copy builtins to constants, remove unused local variables,
etc.
FAT Python implements guards and an optimizer rewriting the Abstract
Syntax Tree (AST). The optimizer is implemented in Python so it's easy
to enhance it and implement new optimizations.
FAT Python uses a static optimizer, it is less powerful than a JIT
compiler like PyPy with tracing, but it was written to be integrated
into CPython.
I wrote 3 PEP (509, 510, 511) targeting Python 3.6. Some changes to
support FAT Python have already been merged into Python 3.6.
We will also see other pending patches to optimize CPython core, and
the bytecode project which allows to modify bytecode, it also includes
a peephole optimizer written in pure Python. |