社内勉強会資料_Two Papers Contribute to Faster Python.pdf

NABLAS 231 views 20 slides Jun 07, 2024
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

社内エンジニア・リサーチャー勉強会の発表資料「Two Papers Contribute to Faster Python」を公開しました!

Pythonの実行速度を向上させるための2つのアプローチ、
Specialized Adaptive InterpreterとCopy-and-patch JIT Compilationについて紹介して�...


Slide Content

Two Papers
Contribute to
Faster Python

Table of Contents
Faster CPython Project

Specializing Adaptive Interpreter

Copy-and-patch JIT Compilation

Faster Python Project

x5 Speed
Python
3.10
Python
3.11
Python
3.12
Python
3.13
Summarized in Implementation plan for speeding up CPython
⬆ 50
%
⬆50
%
⬆50
%
⬆50
%
Table of contents

Specialized Adaptive Interpreter
Motivated by Multi-level Quickening: The Key to Interpreter Performance

Multi-level Quickening: The Key to Interpreter Performance
Background
Interpreters for dynamically-typed programming languages are slow.

CPython
Interpreter
Python
Source
Code
Python
Virtual
Machine
Bytecode

Multi-level Quickening: The Key to Interpreter Performance
First-level Quickening: Type Feedback

Multi-level Quickening: The Key to Interpreter Performance
Second-level Quickening: Type Propagation

Multi-level Quickening: The Key to Interpreter Performance
Optimized Instruction Derivatives
i.e. Make instructions more optimized for machine data types
●Mapping Integer Numbers
○uint64_t ? int64_t ? int32_t
●Mapping Floating Point Number
●Mapping Complex Numbers.

Multi-level Quickening: The Key to Interpreter Performance
Super-instructions:
concatenates frequently occurring sequences of interpreter instructions
>>> dis.dis(add_two)
1 0 RESUME 0
2 LOAD_FAST 0 (a)
4 LOAD_FAST 1 (b)
6 BINARY_OP 0 (+)
10 RETURN_VALUE

>>> dis.dis(add_two, adaptive=True)
1 0 RESUME 0
2 LOAD_FAST__LOAD_FAST 0 (a)
4 LOAD_FAST 1 (b)
6 BINARY_OP_ADD_FLOAT 0 (+)
10 RETURN_VALUE
>>> def add_two(a, b): return a + b

Multi-level Quickening: The Key to Interpreter Performance
Performance
Results

Quickening & Specialization in Reality
Python 3.11
●Specialization clicks in after 8
executions.
●25% faster than Python 3.10.
Python 3.12
●Specialization clicks in after 2
executions.
●Special more new bytecodes.
Table of contents

Copy-and-patch JIT Compilation
Motivated by Copy-and-Patch Compilation: A fast compilation
algorithm for high-level languages and bytecode

Copy-and-Patch Compilation:
A fast compilation algorithm for high-level languages and bytecode
What’s JIT Compilation
●Just in time Compilation, only compile on demand when running for the 1st time.
●In contrast, compiled languages is using “Ahead of time”, compile before being used.

Copy-and-Patch Compilation:
A fast compilation algorithm for high-level languages and bytecode
Interpreting
Parse into
Bytecode
Execution
Copy-and-Patch JIT Compilation
Copy
Bytecode
Execution
Patch
arguments
Running a
function
several
times

Copy-and-Patch Compilation:
A fast compilation algorithm for high-level languages and bytecode
Normal Interpreting

Each time we run a function, CPython parse it into bytecodes, in a loop:
>>> def get_one():
... a = 1
... return a

>>> get_one()

>>> get_one()
1 0 RESUME 0
2 2 LOAD_CONST 1 (1)
4 STORE_FAST 0 (a)
3 6 LOAD_FAST 0 (a)
8 RETURN_VALUE
1 0 RESUME 0
2 2 LOAD_CONST 1 (1)
4 STORE_FAST 0 (a)
3 6 LOAD_FAST 0 (a)
8 RETURN_VALUE

Copy-and-Patch Compilation:
A fast compilation algorithm for high-level languages and bytecode
Copy-and-Patch JIT
>>> def get_one():
... a = 1
... return a
A function with the same
functionality, but written
in a lower language, e.g. C
or Assembly.

Table of contents
Caveat

Copy Instruction is kind of code generation, may expose security concerns.
iOS prohibits such dynamic code generation.

Copy-and-Patch Compilation in Reality
Table of contents
A basic JIT compiler will
be added in Python 3.13
(disabled in beta at the moment)

Take Away
●Upgrade to the latest Python version (official releases)
○Save energy
○Save money
○Save environment

References
●Implementation plan for speeding up CPython
●Multi-Level Quickening: Ten Years Later
●Copy-and-Patch Compilation: A fast compilation algorithm for high-level languages and bytecode
●Python 3.13 gets a JIT