A just-in-time or ahead-of-time optimizer for CPython bytecode. The idea is to perform aggressive, possibly-expensive optimizations on Python functions without trying to compile to native code.
Example:
from cpython_opt import optimize
@optimize(aot=True, level=3)
def test():
a = 3 # Type known to be int
dead_var = 123 # Dead code, eliminated
a *= 1 # Useless operation, removed
def inner_function():
return a + 1
return inner_function() # Inlined
The aot
flag to the optimize
decorator determines whether the function will be compiled eagerly (at function definition time), or when the function becomes "hot" (like a JIT compiler). Delaying compilation until runtime means we can observe the runtime types of values in the function, which could enable further optimizations.
The code in the function can be built into SSA (single static assignment) form. From there, we can eliminate dead code, perform constant propagation and evaluation, and make other deductions that allow further optimizations (e.g. local function inlining), and more.
Some other optimizations worth doing:
- Loop unrolling
- Convert
if
-elif
chains to a jump table - More that I have yet to discover
The compiler has to be pretty pessimistic about whether expressions have side-effects, since it usually doesn't really know what types things have, or what other functions will do. Using the JIT mode can allow it to collect some type information before compiling.