The next release of CPython is likely to include a JIT (just in time) compiler, which according to early tests will bring some immediate speed improvements and the prospect of significant future optimizations.
The new JIT compiler is included in the plan for Python 3.13 and will be optional, requiring the CPython build to be configured with -enable-experimental-jit. The pull request with the initial code was submitted late last month by core Python developer Brandt Bucher. Version 3.13 is scheduled for release in October this year.
JIT compilers are already used by other high level languages such as Java and C# and bring potentially large speed improvements by compiling interpreted code to native machine code at runtime, without requiring any extra steps from the developers.
Among the goals for the JIT compiler are broad platform support, low complexity of implementation, and simple deployment, not adding dependencies for end users installing Python. As described by Bucher in a presentation at a CPython Core Developer Sprint, this is achieved by a technique called Copy-and-Patch Compilation, one proposed in 2021 by researchers at Stanford University. The idea is to stitch together “code from a large library of binary implementation variants. We call these binary implementations stencils because they have holes where missing values must be inserted during code generation,” according to the researchers Haoran Xu and Fredrik Kjolstad.
The copy-and-patch approach is easier to implement for CPython than other JIT-compiler techniques, but still generates high quality code. There is a dependency on LLVM but only when the CPython runtime is built, not for deployment. Platforms supported will include Windows, macOS for both x86 and ARM64, and Linux also for x86 and ARM64.
A key question: how much faster is CPython with the new JIT compiler? “As it stands now, it’s somewhere between 2% and 9% faster than the tier two interpreter, depending on platform (individual benchmarks vary widely, from 13% slower to 47% faster),” said Bucher in a GitHub comment. The quick summary is that we should not expect dramatic speed improvements, though along with other efforts to speed Python it is a worthwhile incremental boost.
That said, moving to a JIT compiler will enable better performance in later versions. Bucher described in another comment some of the potential future optimizations that will improve the generated code, such as “using the ghccc calling convention for more efficient tail calls (way less pushing, popping, and register shuffling at the beginning and end of instructions).” He added that “each of these increases the complexity a tiny bit, and probably deserve to be their own projects that are reviewed individually. I’ve roughly prototyped many of them to prove they’re viable, though.”
The work should be seen as part of a long-term effort to speed Python, and according to Bucher the JIT compiler would not be possible without prior work in CPython 3.11, which added a specializing adaptive interpreter, and in version 3.12, which uses a domain-specific language to generate the interpreter, allowing modification and analysis at build time.