At the Python Language Summit held at PyCon 2021 this week, Python language creator Guido van Rossum unveiled near-term and long-term plans for making Python faster—anywhere from two to five times faster, and possibly more.
The Python language already has many ways to run faster, from alternate runtimes like PyPy to wrapping modules written in C/C++. But almost none of these methods involves speeding up CPython itself—the reference implementation of Python, written in C, that is the most widely used version of the language.
The short-term plan is to add at least one major set of performance improvements to Python 3.11, now officially under development as an alpha-level project. Python 3.11 is slated for release in 2022.
In the presentation given at the Language Summit, van Rossum described how the current plan to speed up Python must operate under some severe constraints. Any changes to CPython must not break the runtime’s ABI (application binary interface), so that Python extensions written in C will continue to work as-is. The changes have to be incremental and manageable, in accordance with CPython’s general goals of preserving maintainability and a straightforward and comprehensible codebase. And all modifications must be open source; there cannot be any proprietary, “black box” extensions to CPython.
Within those constraints, van Rossum and his cohorts identified a few parts of Python that could be changed freely. Python’s bytecode system, compiler, and interpreter have all been singled out as targets, because they tend to change between versions. Bytecode in particular carries with it no guarantee of compatibility across major versions, so it could be changed dramatically if needed.
The first proposals targeted at Python 3.11 include an “adaptive, specializing bytecode interpreter,” as outlined in PEP 659. Bytecode instructions that refer to a specific data type in a particular section of code could be replaced inline with a “specialized” version of that bytecode for that particular data type, engendering a speedup. The developers estimate a potential performance improvement of about 50% in the best cases.
Other suggestions for speed improvements include optimizing the frame stack, changing how function calls are made, implementing more efficient exception handling, adding optimizations that speed startup time, and modifying the .pyc bytecode cache file format.
All of these changes fall short of one of the most commonly suggested improvements to Python: machine-code generation in the runtime, or just-in-time compilation (“JITing”). In his talk, van Rossum suggested that such plans would be considered after Python 3.11, because it made sense to first obtain whatever performance improvements could be had with more targeted changes first.
All of the work being done for this project has been made available on GitHub in a repository, faster-cypthon, with both code (a fork of CPython 3.11) and ideas tracked.