Compilers and interpreters · Part 29 of 36
Bytecode and virtual machines
Expert 18 min
Why interpreters compile to bytecode, how a stack-based VM runs it, and what CPython’s adaptive interpreter (3.11+) changed.
What you will learn
- You can explain why bytecode is faster to run than walking a tree.
- You can trace a small program on a stack machine.
- You can describe CPython’s specialising adaptive interpreter and experimental JIT.
Before this: From text to tree: lexing and parsing
Why not just walk the tree?
A tree-walking interpreter is simple but slow: every node is an object somewhere in memory, every step follows pointers and makes recursive calls. Most production interpreters first compile the AST into bytecode — a compact, linear list of simple instructions — and run that in a virtual machine (VM). Python (CPython), Ruby, Lua, Java and C# (as JVM/.NET bytecode), and JavaScript engines (V8's Ignition) all do this.

A stack machine
Most bytecode VMs are stack machines: instructions take their operands from a value stack and push results back.
price * (1 + tax) compiles to:
LOAD price stack: [price]
CONST 1 stack: [price, 1]
LOAD tax stack: [price, 1, tax]
ADD stack: [price, 1+tax]
MUL stack: [price*(1+tax)]
The VM is a loop around a big switch (or a table of handlers): fetch the next instruction, dispatch on its opcode, execute it. No recursion, good memory locality, and each instruction is small.
You can look at real CPython bytecode:
import dis
dis.dis("price * (1 + tax)")
The exact opcodes change between Python versions — bytecode is an internal format, not a stable interface.
Register machines
Some VMs (Lua since 5.0, Android's Dalvik/ART bytecode, V8's Ignition, which uses an accumulator plus registers) name their operands as virtual registers: ADD r3, r1, r2. Fewer instructions are needed, at the price of bigger instructions. Both designs work; the choice is about dispatch cost.
Dynamic languages are hard to make fast
In a + b, Python doesn't know whether a is an int, a float, a string or a custom object with __add__. Each ADD must check types at run time. That generality is the main cost — but in real programs a given line usually sees the same types again and again.
CPython's specialising adaptive interpreter
Python 3.11 (October 2022) introduced PEP 659: the interpreter watches each instruction, and after a few executions rewrites it in place into a specialised version — for example BINARY_OP becomes BINARY_OP_ADD_INT when it keeps seeing two ints. The specialised instruction checks its assumption cheaply and falls back if it is wrong. Together with other work (cheaper frames, zero-cost exception setup), Python 3.11 was on average about 25 % faster than 3.10 on the standard pyperformance benchmarks — the size depends heavily on the workload.
And a JIT
Python 3.13 (October 2024) added an experimental JIT compiler (PEP 744), off by default, using a "copy-and-patch" technique: machine-code templates for each micro-instruction are prepared when CPython is built and stitched together at run time. As of Python 3.14 it remains experimental and gives modest, workload-dependent gains. PyPy, an alternative Python implementation, has had a tracing JIT for many years and is often several times faster on pure-Python loops.
Ahead-of-time vs just-in-time
- AOT compilation (C, Rust, Go) produces machine code before the program runs: fast start, predictable speed, but no knowledge of run-time types or data.
- JIT compilation compiles while the program runs, using what it observes: it can specialise for the types that actually occur — the topic of the next lesson.
Key points
- Production interpreters compile to bytecode and run it in a VM loop.
- Stack machines push and pop operands; register machines name them.
dis.dis()shows CPython bytecode — an internal, version-specific format.- Python 3.11's adaptive interpreter specialises instructions for observed types (PEP 659).
- Python 3.13 added an experimental copy-and-patch JIT (PEP 744), still experimental in 3.14.
Flashcards
- Question: Why is bytecode faster than tree-walking?
- Answer: It is a compact linear instruction list: no pointer chasing or recursion per node.
- Question: How does a stack machine compute 1 + tax?
- Answer: Push 1, push tax, ADD pops both and pushes the sum.
- Question: What does PEP 659 do?
- Answer: Rewrites instructions into type-specialised versions at run time (adaptive interpreter, Python 3.11).
- Question: Which Python version added an experimental JIT?
- Answer: Python 3.13 (PEP 744).
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Order the stack-machine instructions for 2 * (3 + 4).
Show the answer
CONST 2 → CONST 3 → CONST 4 → ADD → MUL
Operands are pushed first; ADD combines 3 and 4; MUL combines 2 and 7.
Source: Crafting Interpreters, ch. 15
What value is left on the stack after CONST 2, CONST 3, CONST 4, ADD, MUL?
Show the answer
14
3 + 4 = 7, then 2 × 7 = 14.
Source: Crafting Interpreters, ch. 15
What does CPython’s specialising adaptive interpreter do?
- Replaces generic instructions with type-specialised ones after observing them
- Compiles Python to C before running
- Removes the GIL
- Checks types at compile time
Show the answer
Replaces generic instructions with type-specialised ones after observing them
PEP 659 quickens instructions based on observed types, with cheap guards.
Source: PEP 659
Which statements about Python’s JIT are true as of late 2026?
- It was added in Python 3.13
- It uses copy-and-patch
- It is enabled by default and makes all code 10× faster
- It is still described as experimental
Show the answer
It was added in Python 3.13; It uses copy-and-patch; It is still described as experimental
PEP 744: experimental, off by default, modest gains.
Source: PEP 744 / What’s New in Python 3.14
Sources
- Crafting Interpreters, Robert Nystrom, Genever Benning, 2021 (free online)
- PEP 659 – Specializing Adaptive Interpreter, Mark Shannon, 2021
- Python documentation: dis — Disassembler for Python bytecode, Python Software Foundation, 2025
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.