In 2012 Victor Stinner tried a register based bytecode for CPython. It got about 10% on pybench and was abandoned.
Later attempts like RegCPython worked but never landed, because this introduced a lot of crashes in the virtual machine.
I hit the same choice with Edge Python, a sandboxed Python subset in Rust and WebAssembly I posted about in July. Until last week it ran a stack VM and was 3x slower than CPython.
This weekends I switched it to registers and all the math and loops came out the other way.
Ok, so for my case ints and floats are NaN-boxed, so there’s no refcounting and the bytecode is private, so dispatch was most of the cost. And since theres no eval, every name resolves before the program runs. A line like total += i * i % 7 went from 11 instructions per iteration to about 5.
| Edge Python | CPython 3.12 | |
|---|---|---|
| Integer loop | 72 ms | 232 ms |
| Float math | 95 ms | 307 ms |
| Dicts | 108 ms | 62 ms |
| Strings | 111 ms | 35 ms |
Even the wasm build in the browser beats native CPython on the loop. Dicts and strings still lose, so that’s next.
The same idea gave CPython 10% and gave me 3x on loops, because of everything around it. Happy to hear where my reasoning is wrong, I just wanted to tell this story that was very curious for me :).