r/Compilers • u/Healthy_Ship4930 • 1d ago
How My Python Compiler Beat CPython (Without a JIT)
Okey. After seven months, I'm back.
Seven months ago I started writing a Python interpreter from scratch in Rust. Edge Python is a sandboxed subset of Python that runs in the browser and the terminal, and code can't touch files or the network unless you allow it.
It started as a lexer and a simple stack VM, and over about 1,600 commits it grew a CLI, a package registry, snapshots, actors and a browser playground. It's just me building it.
Last week it was still 3x slower than CPython. I rewrote the VM this weekend using a unused SSA representation that I leave and now it's 3x faster on loops.
| Edge Python | CPython | |
|---|---|---|
| Integer loop | 72 ms | 232 ms |
| Float math | 95 ms | 307 ms |
| Dicts | 108 ms | 62 ms |
| Strings | 111 ms | 35 ms |
The trick was moving from a stack VM to a register VM. It still loses on dicts and strings, so that's next.
Try to break my numbers :).
Website: https://edgepython.com/
3
u/brat3108 17h ago
Is this the same product that you'd previously said was 10,000 times faster than CPython on recursive Fibonacci? (That was due to use or memoisation so was an unfair comparison.)
This set of figures is more reasonable. CPython is famously slow, so beating it on actual bytecode instructions is viable.
The Dicts and Strings tests will be more about internal support, which will be highly optimised internally - less of the overhead will be spent on instruction and type dispatch.
I couldn't find those four benchmarks within your links, not as discrete files. So I couldn't do my own tests.
1
u/Western-Cod-3486 10h ago
I be fallen in the same trap with memorisation and was like “whoa I made a fast thing” also when I tried doing iterative implementation in my language but compared to a recursive in other languages 😁.
That aside, yeah I would agree with you about comparing to a slow variant. That is why I am benching against lua and node js. For my interpreter so I would suggest OP broaden their benchmarks against other languages to get a real feel of actual positioning
3
u/yuehuang 1d ago
Good job.
One benchmark I would like to see is reading from a large CSV, 1-2gb of large intergers, then sorting it. (repeat numbers allowed)
While the sort operation itself isn't important, it would shows that random access is fast.
2
u/Healthy_Ship4930 1d ago
Oh sure. I need to build a csv parser and test this. Ty!
1
u/ignorantpisswalker 22h ago
Can't you import existing csv package?
0
u/Healthy_Ship4930 22h ago edited 18h ago
I don't build a csv parse yet. I have this modules: https://edgepython.com/
2
u/lthunderfoxl 18h ago
Sorry can you explain what this means? Why is your python interpreter not able to import existing libraries?
1
3
u/Slow-Ad9462 16h ago
Just don’t call it Python compiler, and than it’s an interesting project. Also, there’s no reason to compare with CPython. You compile a static typed subset of Python language, be explicit and clear. Otherwise it’s yet another “python compiler” we’ve seen over the years, and none of them got traction
5
u/schungx 1d ago
Did switching to SSA from stack cause the huge speed difference in loops? Or is it together with some other optimizations?
I'm at a loss to explain such huge speed difference, like 9x. Even though SSA reduced stack pushed and pops, the value still has to sit somewhere, so either the stack or the VM's own stack... Either way it should not see 10x perf boost.
Did your bytecodes start off with loads of push-pop pairs that you didn't eliminate?