Father of Python: Doubling Python’s Speed

This article is authorized for reprint by AI new media Quantum Bit (ID: QbitAI). Please contact the source for reprinting.
Written by Bowen from Aofeisi

At Pycon US 2021, which just concluded in mid-May, the father of Python, Guido van Rossum, proposed to increase the speed of CPython by five times over the next four years.

Father of Python: Doubling Python's Speed

Participants in this “Shannon Plan” include not only Guido himself but also Eric Snow, a core CPython developer at Microsoft, and Mark Shannon, a research engineer at Semmle.

However, before this, Guido did not consider improving the speed of CPython to be critical, as “there are other ways to achieve better performance,” such as using JIT compilation with PyPy or writing extensions in C.

Why Accelerate?

Is Python really slow?

Not necessarily; it is inherently difficult to balance development efficiency and execution speed.

Moreover, Python has evolved into a glue language, primarily used to quickly build the logical control flow of systems, while delegating performance-critical parts to C/C++ for implementation.

However, if we only look at the standard implementation of the language itself… its performance is indeed not impressive.

Father of Python: Doubling Python's Speed

The characteristics of dynamic languages mean that Python spends a lot of time on C language code execution (runtime) and is difficult to optimize using JIT (Just-In-Time).

In an interview with the UK technology news site The Register, when asked, “Why start focusing on CPython performance?” Shannon stated:

In recent years, the usage of Python in machine learning has greatly increased, and available resources have also grown. This means we can focus on performance without worrying about compromising its reliability.

Additionally, the interpreter developed in the HotPy project, which Shannon previously participated in, is three times faster than the current CPython interpreter’s pure Python code. This proves the feasibility of optimizing CPython.

Last October, Guido, unable to resist retirement boredom, joined Microsoft:

Father of Python: Doubling Python's Speed

With the added time from the pandemic, the big names came together and decided to Make Python Great Again.

Challenges Before Acceleration

Shannon admitted that backward compatibility is the biggest challenge in accelerating Python.

This is not only true for Python; the incompatible update of libc in the late 1990s directly led to the need to recompile all applications…

Currently, the now-defunct Pyston mentioned several factors in its official article regarding Dropbox’s abandonment of the Pyston project, the first of which is:

Father of Python: Doubling Python's Speed

This is a severe problem that all attempts to maintain compatibility with CPython while significantly improving performance will encounter.

Because Python’s execution is similar to HTML rendering: it describes more how to execute C libraries at runtime rather than executing commands step by step.

Thus, the source of Python’s performance improvement comes from these C extension modules. CPython has over 400k lines of code, which means optimizing from the bottom up is a massive undertaking.

Especially for the overly dynamic Python language, the semantics of the language have a greater impact on optimization.

Now, during the acceleration process, how many tools, debuggers, configuration files, NumPy packages, and compilers like Cython will involve CPython’s internal and low-level behavior?

Therefore, Shannon stated:

Changing things is difficult… the implicit agreements among CPython users do not clearly define what can be changed and what cannot.

Perhaps the painful experience of migrating from Python 2.x to 3 five years ago is still fresh, as Guido specifically tweeted that this migration would be more peaceful.

Father of Python: Doubling Python's Speed

He also promised at the Python summit: not to break stable ABI compatibility; not to break limited API compatibility; not to break or slow down extreme cases.

“In short, code maintainability is the top priority.”

The Road to Python’s 5x Speed

According to the faster-cpython project already published on GitHub, Shannon’s plan is divided into four phases:

Python 3.10

Expected to be released in October this year, mainly adding an adaptive, specialized interpreter.

Father of Python: Doubling Python's Speed

The interpreter will no longer generate code at runtime but will adapt types and values during execution based on the stability of types in the program.

Python 3.11

Guido proposed to achieve at least a 2x speedup in version 3.11, for which he has already submitted a proposal for enhancements, PEP 659.

Father of Python: Doubling Python's Speed

This proposal indicates the addition of an adaptive bytecode interpreter and the implementation of more effective exception handling.

In addition, it proposes to optimize the frame stack, change the way functions are called, add optimizations to speed up startup time, and modify the .pyc bytecode cache file format.

Father of Python: Doubling Python's Speed

Python 3.12

This phase will use a JIT interpreter for small areas, compiling specialized code quickly and simply during code execution.

Python 3.13

This phase will also compile extended areas during code execution, enhancing the compiler to achieve a 5x super acceleration.

Guido stated that the changes to Python focused on performance will primarily serve developers running CPU-intensive pure Python code and users of built-in Python websites.

However, the improvement effect on C language code (such as NumPy and TensorFlow), I/O-bound code, multithreaded code, and algorithm code will be relatively limited.

Microsoft’s Contribution to Python

In fact, Microsoft has long supported Python projects in various ways, including releasing free Python courses in Azure cloud AI service tutorials and supporting Python through the VS Code Python extension on Windows 10 and above.

Since 2006, Microsoft has also been a sponsor of the Python Software Foundation (PSF) and has contributed $150,000 this year.

Currently, five core members of the Python developer community are employed at Microsoft, including the father of Python who joined at the end of last year and Eric Snow, one of the three in the Shannon plan.

Guido also specifically mentioned Microsoft at this summit, stating that Microsoft funded a small Python team “responsible for performance improvements at the language interpretation level,” allowing him to continue developing Python alongside his Microsoft colleagues.

Father of Python: Doubling Python's Speed

Of course, regarding the short-term goals for version 3.11, Guido still gave himself a safety net in the ppt.

Father of Python: Doubling Python's Speed

“Being a bit optimistic and curious is never wrong”

As for the ultimate goal of five times speed in four years, Guido stated, “We must maintain strong creativity.”

Father of Python: Doubling Python's Speed

Reference links:[1]https://www.theregister.com/2021/05/19/faster_python_mark_shannon_author/[2]https://pyfound.blogspot.com/2021/05/the-2021-python-language-summit-pep-654.html[3]https://www.python.org/dev/peps/pep-0654/

Python version 3.11.0 alpha 0:https://github.com/faster-cpython/ideasIntroduction to the “A faster CPython” plan:https://github.com/markshannon/faster-cpython

— EOF —

Leave a Comment