How Python 3.15 Watches Itself Run, For Almost Free
4 months ago
To make Python faster, its new compiler watches the interpreter run. But watching a running program normally slows the thing you measure. This one barely does.
Ask about this presentation
Chapters
0:00How Python 3.15 Watches Itself Run, For Almost Free
0:09Its new JIT records the interpreter, then compiles the path.
0:24Three ways to watch. Two of them are too slow.
0:38Keep two dispatch tables. Swap one pointer.
1:10On a toy benchmark: at most 4.5× — profiling and compiling together.
1:23The cost didn't vanish. It moved.
1:37When you must watch every step, do not check at every step.
Show transcriptHide transcript
To make Python faster, its new compiler watches the interpreter run. But watching a running program normally slows the thing you measure. This one barely does.
Python 3.15 ships an experimental compiler. To know what to compile, it records the exact stream of instructions the interpreter runs. But recording means watching every instruction. And the watcher has to stay cheap.
There are three ways to build that watcher. The first two are too slow. One runs a second interpreter beside the normal one, costing about six percent. The other checks every instruction. Both grow the hot loop, and it has to stay small.
Here's the third way. An interpreter picks where to go next by looking each instruction up in a table of addresses. Keep two tables. One variable says which is live. So switching modes is one pointer swap, with no branch on any instruction. Now the clever part. Every instruction in the profiling table points at one recorder. It logs the step, then dispatches through the normal table to the real handler. Off, the interpreter runs as before. Turning it on, or off, is one line.
On a toy benchmark, profiling and compiling together ran at most four and a half times slower than the plain interpreter. That cost is real, but small, and only while recording. The switch itself is free.
The runtime cost nearly vanished. But it did not disappear. It moved. The interpreter now carries two dispatch tables and a fan-in for a person to keep straight. The author calls it elegant, and hard to reason about.
The lesson has nothing to do with Python. When you must watch every step, do not check at every step. Swap the rulebook. Route every step through one watcher.
Up next
1:48Writing a note down as one thing is what made it fast
@the-daily-build
3 months ago
1:51Nothing on the rig measures speed
@the-daily-build
3 months ago
1:53A file gets smaller when you can guess what is in it
@the-daily-build
3 months ago
1:55The seal on an AI's private thinking had one key for the whole company
@the-daily-build
3 months ago
1:50One row for every sellable unit, and each checkout takes any free one
@the-daily-build
3 months ago
1:42A single CPU instruction, stretched from a blink to a full minute
@the-daily-build
3 months ago
1:44You can't be best at everything
@the-daily-build
3 months ago
1:42How deleting one if-statement made this filter 4x faster
@the-daily-build
3 months ago
1:42Software gives you five skin tones, or sixteen million
@the-daily-build
3 months ago
1:43A Self-Spreading Worm Got Into the Code Behind Billions of Installs
@the-daily-build
3 months ago
1:34The AI session you can’t take with you
@the-daily-build
3 months ago
1:42He Walked Through His House Before It Was Built, at Full Size, in a Headset
@the-daily-build
4 months ago