Back
+20 XP
Concept

CPython 3.13 Tier 2 JIT: Copy-and-Patch Architecture

Python 3.13 introduces an experimental Tier 2 JIT compiler. Using lightweight copy-and-patch technology, hot bytecode traces identified by Tier 1 adaptive interpreters are lowered into micro-ops (uops) and emitted into native machine code stencils without LLVM compilation latency 🏎️⚙️.

python
import dis

def hot_loop(iterations):
    total = 0
    for i in range(iterations):
        total += i
    return total

# Warm up function to trigger adaptive specialization:
for _ in range(100):
    hot_loop(10)

# Inspect instructions with inline caches:
instructions = list(dis.get_instructions(hot_loop, show_caches=True))
print(f"Specialized instructions emitted: {len(instructions)} opcodes 🚀")

# 77. CPython 3.13 Tier 2 JIT: Copy-and-Patch Architecture For decades, adding a JIT compiler to CPython was rejected because traditional JIT compilers (like LLVM) are gigantic, slow to compile, and consume hundreds of megabytes of memory just to compile a simple loop 🛑. ```python # The CPython Multi-Tier Execution Pipeline: # Tier 1 (Bytecode Interpreter): Adaptive Specialization (PEP 659) # | (Identifies hot loops with execution counters) # Tier 2 (Optimizer): Lowers bytecode into Micro-Operations (uops) # | (Removes redundant type checks and bounds checks) # Tier 2 JIT (Copy-and-Patch): Emits native x86/ARM machine code! ``` ### Copy-and-Patch JIT Technology 1. **Pre-Compiled Stencils**: At CPython build time, Clang compiles small C functions (one for each micro-op) into machine code templates (stencils) with holes where dynamic values (like memory addresses and constants) go. 2. **Microsecond Code Generation**: At runtime, when a trace becomes hot, the JIT simply `memcpy`s the pre-compiled stencils into an executable memory buffer, patches in the constants, and jumps directly to machine code 🏎️. 3. **No Heavy Compiler Dependency**: The runtime JIT engine is tiny (a few thousand lines of C), requires zero third-party dependencies, and produces near-instant native code ⚙️. --- ### Interactive Pop-Quiz 🧠 #### What is the primary advantage of the 'Copy-and-Patch' JIT architecture adopted by CPython 3.13 over traditional heavy JITs like LLVM? - [ ] Pre-compiles machine code stencils with Clang at build-time, allowing runtime code generation in microseconds with zero heavy compiler runtime 🏎️ - [ ] It executes Python code on the GPU without CPU memory - [ ] It converts dynamic Python variables into static C++ templates - [ ] It replaces the Python interpreter loop with a Bash script --- ### Practical Challenge 🎯 Define a function `multiply_accumulate(a, b)`. Warm it up by running it in a loop 50 times with integers. Inspect its instructions using `dis.get_instructions(multiply_accumulate)`. Find and print the name of the binary arithmetic opcode used (e.g. `'BINARY_OP'`). <details> <summary>Solution</summary> ```python import dis def multiply_accumulate(a, b): return a * b for _ in range(50): multiply_accumulate(3, 4) opcodes = [instr.opname for instr in dis.get_instructions(multiply_accumulate)] match = next(name for name in opcodes if "BINARY" in name or "MULTIPLY" in name) print(match) ``` </details>

Checkpoint

What is the primary advantage of the 'Copy-and-Patch' JIT architecture adopted by CPython 3.13 over traditional heavy JITs like LLVM? 🧠

Practice

Define a function `multiply_accumulate(a, b)`. Warm it up by running it in a loop 50 times with integers. Inspect its instructions using `dis.get_instructions(multiply_accumulate)`. Find and print the name of the binary arithmetic opcode used (e.g. `'BINARY_OP'`). 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks