avatarDylan Cooper

Summary

Python 3.13 is set to include JIT (Just-In-Time Compiler) as an optional feature, with early testing showing significant improvements in runtime speed and promising prospects for further optimizations.

Abstract

According to reliable sources, the next version of CPython, Python 3.13, is set to include JIT (Just-In-Time Compiler) as an optional feature. Based on early testing by some users, it is expected to bring significant improvements in runtime speed directly, with promising prospects for further optimizations in the future. The new JIT compiler will be integrated into Python 3.13 as an optional feature, requiring developers to configure CPython builds with -enable-experimental-jit.

Opinions

  • JIT compilation is a technique aimed at improving program performance, and PyPy is a Python interpreter that implements JIT compilation to optimize the execution speed of Python code.
  • The goals of the JIT compiler include widespread operating system support, low complexity of implementation, straightforward deployment, and avoiding dependencies for end-users installing Python.
  • The JIT technology is based on the LLVM ecosystem, with the compiler using clang and compilation parameters set to -O3 for maximum performance.
  • Supported operating systems include Windows, macOS for x86 and ARM64, as well as Linux for x86 and ARM64.
  • The next steps in migrating to the JIT compiler are expected to bring even better performance in future versions.
  • This effort is considered part of the long-term endeavor to accelerate Python.
  • The new JIT compiler is expected to bring a commendable incremental improvement working alongside other tools to accelerate Python.

Revolutionizing Performance: JIT Compilation Coming to Python 3.13!

According to reliable sources, the next version of CPython is set to include JIT (Just-In-Time Compiler).

Based on early testing by some users, it is expected to bring significant improvements in runtime speed directly, with promising prospects for further optimizations in the future. The new JIT compiler will be integrated into Python 3.13 as an optional feature, requiring developers to configure CPython builds with -enable-experimental-jit.

01 What is JIT?

JIT, or Just-In-Time Compilation, is a compilation technique that transforms source code or intermediate code into machine code during program runtime. Unlike traditional static compilation, JIT defers the compilation of code to a phase during program execution. This helps enhance the efficiency of program execution by allowing the compiler to optimize the code based on runtime information.

In JIT compilation, the program’s code is initially interpreted by an interpreter. Then, based on runtime information, certain portions of the code are selectively compiled into machine code to improve execution speed. This hybrid execution approach combines the flexibility of interpreted execution with the performance advantages of just-in-time compilation.

In the context of Python, the CPython interpreter is an interpreter that translates Python code into bytecode. However, Python also has some JIT implementations, with PyPy being a notable example. PyPy is a Python interpreter that utilizes JIT compilation technology to convert Python code into machine code, thereby enhancing execution speed.

Unlike CPython, PyPy is designed to leverage JIT compilation more effectively. By performing just-in-time compilation of Python code, PyPy aims to improve overall program performance. In certain scenarios, PyPy can execute the same Python code faster than CPython, particularly in computationally intensive tasks.

In summary, JIT compilation is a technique aimed at improving program performance, and PyPy is a Python interpreter that implements JIT compilation to optimize the execution speed of Python code.

02 Current Progress

Kernel Python developer Brandt Bucher submitted a pull request with initial code at the end of last month. The 3.13 version is scheduled for release in October 2024.

JIT compilers have been employed by other high-level languages, such as Java, PHP, and C#, to achieve significant speed improvements by compiling interpreted code into native machine code at runtime, eliminating the need for developers to perform any additional steps.

The goals of the JIT compiler include widespread operating system support, low complexity of implementation, straightforward deployment, and avoiding dependencies for end-users installing Python.

As described by Bucher during the CPython Core Developer Sprint, this is achieved through a technique known as “Copy and Patch Compilation,” proposed by researchers Haoran Xu and Fredrik Kjolstad from Stanford University in 2021. The idea is to “splice together code from large binary implementation variant libraries.” These binary implementations are referred to as templates, as they contain vulnerabilities that must be patched in during the code generation process.

Compared to other JIT compiler technologies, the Copy and Patch approach is easier to implement for CPython while still generating high-quality code.

The JIT technology is based on the LLVM ecosystem, with the compiler using clang and compilation parameters set to -O3 for maximum performance. However, it only exists during the CPython runtime build and not during deployment. Supported operating systems include Windows, macOS for x86 and ARM64, as well as Linux for x86 and ARM64.

03 How Much Faster Is CPython with the New JIT Compiler?

Bucher mentioned in a GitHub comment:

As of now, it’s 2% to 9% faster than the two-step interpreter, depending on the platform (benchmarks vary widely, ranging from 13% slower to 47% faster).

In summary, we shouldn’t expect a particularly significant speed boost, although it’s a commendable incremental improvement working alongside other tools to accelerate Python.

The next steps in migrating to the JIT compiler are expected to bring even better performance in future versions. Bucher highlighted some potential optimizations, aiming to enhance the generated code, such as “using the ghccc calling convention to achieve more efficient tail calls (reduce pushes, pops, and register shuffling at the beginning and end of instructions). Each adds a bit of complexity and should probably be its own review item. However, I’ve roughly prototyped many of them, demonstrating their feasibility.”

According to Bucher, this work builds upon the foundation laid in CPython 3.11 (introduction of a dedicated adaptive interpreter) and 3.12 (use of a domain-specific language to generate the interpreter, allowing modifications and analysis during build).

This effort is considered part of the long-term endeavor to accelerate Python.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

Technews
Technology
Python
Python Programming
Data Science
Recommended from ReadMedium