Home  >  Article  >  Backend Development  >  The goal of Python 3.12: more efficient performance!

The goal of Python 3.12: more efficient performance!

王林
王林forward
2023-04-22 14:43:091681browse

Python 3.12 目标:还可以更快!

According to the release plan, Python 3.11.0 will be released on October 24, 2022.

According to testing, 3.11 will have a 10-60% performance improvement compared to 3.10. This achievement is mainly attributed to the "Faster CPython" project, also known as the "Shannon Project".

The 3.11 version is an exciting start for Python’s speedup. More action is coming in 3.12 next.

The following article is translated from "Python 3.12 Goals" by "Shannon Project", let's take a look first!

  • Author: Mark Shannon
  • Translator: Pea Flower Cat @Python Cat
  • English: https://github.com/faster-cpython/ideas /wiki/Python-3.12-Goals

The content of this article may be changed, the actual version shall prevail!

This article is a summary of the main content that Faster CPython plans to implement in 3.12.

Trace Optimizer

The main way to improve speed in Python 3.11 is to replace individual opcodes with faster context-sensitive opcodes (adaptive specialization opcodes), next A big improvement would be to optimize the running of multiple opcodes.

To do this, many of the existing high-level opcodes will be replaced with low-level opcodes, such as those used to check version numbers and reference counts. These simpler opcodes are easier to optimize, for example, redundant reference counting operations can be removed.

These lower-level opcodes also give us a set of instructions suitable for generating machine code (in both CPython and third-party JIT projects).

To do this, an interpreter loop will be generated based on the declarative description.

This can reduce some of the bugs caused by keeping the interpreter loop synchronized with certain related functions (mark_stacks, stack_effect, etc.), and also allows us to experiment with larger changes to the interpreter loop.

Multi-threaded parallelism

Python currently has a global interpreter lock (GIL) per process, which hinders multi-threaded parallelism.

  • PEP-684: https://peps.python.org/pep-0684
  • PEP-554: https://peps.python.org/pep-0554

Python 3.12 目标:还可以更快!

PEP-684 proposed a solution to ensure that all global state is thread-safe and moved to the global interpreter lock of each sub-interpreter use.

Python 3.12 目标:还可以更快!


PEP-554 proposes a solution for Python to create a sub-interpreter (currently only a C API feature), thereby achieving true Multi-threaded parallelism.

  • Python Cat Note: PEP-554 was proposed as early as 2017, with the goal of landing in Python 3.8-3.9 versions, but things went counterproductive. As early as 2019, I also translated an article "Has the Python GIL been slain?". The butcher knife has been swung, let it fly for a while~~

More specialization

We analyzed which bytecodes will benefit the most from specialization, and plan to 3.12 Complete the remaining high-yield improvements.

​https://www.php.cn/link/7392ea4ca76ad2fb4c9c3b6a5c6e31e3​

Python 3.12 目标:还可以更快!

Smaller object structure

There are many opportunities to reduce the size of Python object structures. Since they are used frequently, this not only benefits overall memory usage, but also cache consistency. We plan to implement some of the most promising ideas in 3.12.

There are some trade-offs between backward compatibility and performance, and a PEP may need to be proposed to establish consensus.

Python 3.12 目标:还可以更快!

Reduce memory management overhead

We will not only reduce the size of objects, but also make their layout more regular.

This not only optimizes memory allocation and deallocation, but also speeds up object traversal during GC and reallocation.

API Stability

In addition to the aforementioned projects, the development team will also improve the overall quality of the CPython codebase:

  • By reducing the coupling of different compilation stages, the compiler is easier to maintain and test.
  • Actively monitor and improve code coverage of CPython test suites at the C language level.
  • Improve the Python performance benchmark suite to include more representative real-world load tests.
  • Assist with CPython issues and PRs, especially those related to performance.
  • Added machines for standard benchmarks and added test results for macOS and Windows.
  • Continue to work with major projects that heavily use the Python kernel to help them adapt to changes in the CPython interpreter.

The above is the detailed content of The goal of Python 3.12: more efficient performance!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete