Skip to content

Instantly share code, notes, and snippets.

@p7g
Last active November 24, 2020 23:22
Show Gist options
  • Save p7g/a3600722be15080520fc8406d51e1fc4 to your computer and use it in GitHub Desktop.
Save p7g/a3600722be15080520fc8406d51e1fc4 to your computer and use it in GitHub Desktop.
Python optimizer

cpython-opt (idea)

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

How it works

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment