|
| 1 | +# Python 3.13 Preview: Free Threading and a JIT Compiler |
| 2 | + |
| 3 | +This repository contains sample code and data files for the [Python 3.13 Preview: Free Threading and a JIT Compiler](https://realpython.com/python313-free-threading-jit/) tutorial on Real Python. |
| 4 | + |
| 5 | +## Build the Docker Image |
| 6 | + |
| 7 | +Build Python 3.13 from source code using Docker: |
| 8 | + |
| 9 | +```sh |
| 10 | +$ docker image build --tag pythons . |
| 11 | +``` |
| 12 | + |
| 13 | +**Note:** If you want to compile your C extension modules, then you'll need to keep the intermediate stage with the required build tools: |
| 14 | + |
| 15 | +```sh |
| 16 | +$ docker build --target stock-builder --tag builder . |
| 17 | +$ docker image build --tag pythons . |
| 18 | +``` |
| 19 | + |
| 20 | +## Run Performance Benchmarks Using Docker |
| 21 | + |
| 22 | +Change directory to the `benchmarks/` folder: |
| 23 | + |
| 24 | +```sh |
| 25 | +$ cd benchmarks/ |
| 26 | +``` |
| 27 | + |
| 28 | +### Free Threading |
| 29 | + |
| 30 | +Run the benchmark against Python 3.12 shipped with the system: |
| 31 | + |
| 32 | +```sh |
| 33 | +$ docker run --rm -v "$(pwd)":/app -w /app pythons python3.12 gil.py --threads=4 |
| 34 | +====================================================== |
| 35 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 36 | +🐍 CPython 3.12.3 /usr/bin/python3.12 |
| 37 | +Free Threading: unsupported |
| 38 | +JIT Compiler: unsupported |
| 39 | +====================================================== |
| 40 | +Running 4 threads: 7.33s |
| 41 | +``` |
| 42 | + |
| 43 | +Run the benchmark against stock Python 3.13: |
| 44 | + |
| 45 | +```sh |
| 46 | +$ docker run --rm -v "$(pwd)":/app -w /app pythons python3.13 gil.py --threads=4 |
| 47 | +====================================================== |
| 48 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 49 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13 |
| 50 | +Free Threading: unsupported |
| 51 | +JIT Compiler: unsupported |
| 52 | +====================================================== |
| 53 | +Running 4 threads: 7.51s |
| 54 | +``` |
| 55 | + |
| 56 | +Run the benchmark against the free-threaded Python 3.13 build with the GIL and JIT disabled: |
| 57 | + |
| 58 | +```sh |
| 59 | +$ docker run --rm -v "$(pwd)":/app -w /app -e PYTHON_JIT=0 pythons python3.13t gil.py --threads=4 |
| 60 | +====================================================== |
| 61 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 62 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13t |
| 63 | +Free Threading: enabled ✨ |
| 64 | +JIT Compiler: disabled |
| 65 | +====================================================== |
| 66 | +Running 4 threads: 3.25s |
| 67 | +``` |
| 68 | + |
| 69 | +Run the benchmark against the free-threaded Python 3.13 build with the GIL enabled and JIT disabled: |
| 70 | + |
| 71 | +```sh |
| 72 | +$ docker run --rm -v "$(pwd)":/app -w /app -e PYTHON_JIT=0 pythons python3.13t -X gil=1 gil.py --threads=4 |
| 73 | +====================================================== |
| 74 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 75 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13t |
| 76 | +Free Threading: disabled |
| 77 | +JIT Compiler: disabled |
| 78 | +====================================================== |
| 79 | +Running 4 threads: 13.72s |
| 80 | +``` |
| 81 | + |
| 82 | +### Just-In-Time (JIT) Compiler |
| 83 | + |
| 84 | +Run the benchmark against Python 3.12 shipped with the system: |
| 85 | + |
| 86 | +```sh |
| 87 | +$ docker run --rm -v "$(pwd)":/app -w /app pythons python3.12 jit.py -n 10000 |
| 88 | +====================================================== |
| 89 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 90 | +🐍 CPython 3.12.3 /usr/bin/python3.12 |
| 91 | +Free Threading: unsupported |
| 92 | +JIT Compiler: unsupported |
| 93 | +====================================================== |
| 94 | +Running fib() 10,000 times: 6.06s |
| 95 | +``` |
| 96 | + |
| 97 | +Run the benchmark against stock Python 3.13: |
| 98 | + |
| 99 | +```sh |
| 100 | +$ docker run --rm -v "$(pwd)":/app -w /app pythons python3.13 jit.py -n 10000 |
| 101 | +====================================================== |
| 102 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 103 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13 |
| 104 | +Free Threading: unsupported |
| 105 | +JIT Compiler: unsupported |
| 106 | +====================================================== |
| 107 | +Running fib() 10,000 times: 5.68s |
| 108 | +``` |
| 109 | + |
| 110 | +Run the benchmark against Python 3.13 with the JIT enabled but GIL unsupported: |
| 111 | + |
| 112 | +```sh |
| 113 | +$ docker run --rm -v "$(pwd)":/app -w /app pythons python3.13j jit.py -n 10000 |
| 114 | +====================================================== |
| 115 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 116 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13j |
| 117 | +Free Threading: unsupported |
| 118 | +JIT Compiler: enabled ✨ |
| 119 | +====================================================== |
| 120 | +Running fib() 10,000 times: 5.27s |
| 121 | +``` |
| 122 | + |
| 123 | +## Disassemble JIT's Micro-Ops |
| 124 | + |
| 125 | +Reveal mico-ops generated by the JIT compiler: |
| 126 | + |
| 127 | +```sh |
| 128 | +$ cd benchmarks/ |
| 129 | +$ docker run --rm -it -v "$(pwd)":/app -w /app pythons python3.13j -i uops.py |
| 130 | +====================================================== |
| 131 | +💻 Linux 64bit with 4x CPU cores (x86_64 Little Endian) |
| 132 | +🐍 CPython 3.13.0rc1 /usr/local/bin/python3.13j |
| 133 | +Free Threading: unsupported |
| 134 | +JIT Compiler: enabled ✨ |
| 135 | +====================================================== |
| 136 | +>>> def fib(n): |
| 137 | +... a, b = 0, 1 |
| 138 | +... for _ in range(n): |
| 139 | +... a, b = b, a + b |
| 140 | +... return a |
| 141 | +... |
| 142 | + |
| 143 | +>>> fib(10) |
| 144 | +55 |
| 145 | + |
| 146 | +>>> reveal_code(fib) |
| 147 | +Micro-ops unavailable |
| 148 | + |
| 149 | +>>> fib(10) |
| 150 | +55 |
| 151 | + |
| 152 | +>>> reveal_code(fib) |
| 153 | +_START_EXECUTOR |
| 154 | +_TIER2_RESUME_CHECK |
| 155 | +_ITER_CHECK_RANGE |
| 156 | +_GUARD_NOT_EXHAUSTED_RANGE |
| 157 | +_ITER_NEXT_RANGE |
| 158 | +_STORE_FAST_3 |
| 159 | +_LOAD_FAST_2 |
| 160 | +_LOAD_FAST_1 |
| 161 | +_LOAD_FAST_2 |
| 162 | +_GUARD_BOTH_INT |
| 163 | +_BINARY_OP_ADD_INT |
| 164 | +_STORE_FAST_2 |
| 165 | +_STORE_FAST_1 |
| 166 | +_JUMP_TO_TOP |
| 167 | +_DEOPT |
| 168 | +_EXIT_TRACE |
| 169 | +_EXIT_TRACE |
| 170 | +_ERROR_POP_N |
| 171 | +_EXIT_TRACE |
| 172 | +_ERROR_POP_N |
| 173 | +``` |
| 174 | +
|
| 175 | +## Compile the C Extension Module |
| 176 | +
|
| 177 | +Build an intermediate stage with the necessary build tools if you haven't before: |
| 178 | +
|
| 179 | +```shell |
| 180 | +$ docker build --target stock-builder --tag builder . |
| 181 | +``` |
| 182 | +
|
| 183 | +Change directory to the `c_extension/` folder: |
| 184 | +
|
| 185 | +```sh |
| 186 | +$ cd c_extension/ |
| 187 | +``` |
| 188 | +
|
| 189 | +Start a Docker container from the builder stage and set common compiler flags: |
| 190 | +
|
| 191 | +```sh |
| 192 | +$ docker run --rm -it -v "$(pwd)":/app -w /app builder |
| 193 | +root@8312f61fbb6d:/app# CFLAGS='-shared -fPIC -O3' |
| 194 | +``` |
| 195 | +
|
| 196 | +Compile the `greeter` module for stock Python 3.13: |
| 197 | +
|
| 198 | +```sh |
| 199 | +root@8312f61fbb6d:/app# gcc greeter.c $CFLAGS \ |
| 200 | + $(python3.13-config --includes) \ |
| 201 | + -o greeter_stock.so |
| 202 | +``` |
| 203 | +
|
| 204 | +Compile the `greeter` module for the free-threaded Python 3.13: |
| 205 | +
|
| 206 | +```sh |
| 207 | +root@8312f61fbb6d:/app# gcc greeter.c $CFLAGS \ |
| 208 | + $(python3.13t-config --includes) \ |
| 209 | + -o greeter_threaded_v1.so |
| 210 | +``` |
| 211 | +
|
| 212 | +Compile the `greeter_threaded` module for the free-threaded Python 3.13: |
| 213 | +
|
| 214 | +```sh |
| 215 | +root@8312f61fbb6d:/app# gcc greeter_threaded.c $CFLAGS \ |
| 216 | + $(python3.13t-config --includes) \ |
| 217 | + -o greeter_threaded_v2.so |
| 218 | +``` |
0 commit comments