r/Compilers • • Oct 05 '26

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 PythonCPython
Integer loop72 ms232 ms
Float math95 ms307 ms
Dicts108 ms62 ms
Strings111 ms35 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/

GitHub: https://github.com/dylan-sutton-chavez/edge-python

20 Upvotes

16 comments sorted by

5

u/schungx Oct 05 '26

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?

1

u/Healthy_Ship4930 Oct 05 '26

Fair point. And yeah, the old virtual machine hashed every module level lookup. Inside functions the gain is closer to 3x.

And I dropped some unecessary code-paths.

3

u/schungx Oct 05 '26

Did you change it to a register file with accumulator or did you really switch to a fixed number of registers and spill on stack?

I'm wondering because it would help only if your instructions are built in. If your scripts rely on lots of function calls, you're probably down to preparing the stack for the call anyway... Or did you have an ABI for functions?

3

u/Patzer26 Oct 05 '26

SSA enabled instruction parallelism and pipelining which is just not possible in a stack VM. This is a justifiable speed up.

1

u/kronicum Oct 05 '26

SSA also introduces lot of superfluous copies and redundancies, and needs separate passes to eliminate them. I am curious if the OP can report on those.

6

u/yuehuang Oct 05 '26

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 Oct 05 '26

Oh sure. I need to build a csv parser and test this. Ty!

1

u/ignorantpisswalker Oct 05 '26

Can't you import existing csv package?

0

u/Healthy_Ship4930 Oct 05 '26 edited Oct 05 '26

I don't build a csv parse yet. I have this modules: https://edgepython.com/

2

u/lthunderfoxl Oct 05 '26

Sorry can you explain what this means? Why is your python interpreter not able to import existing libraries?

2

u/Healthy_Ship4930 Oct 05 '26

because I don't build a pyobjects based abi

5

u/[deleted] Oct 05 '26

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 Oct 05 '26

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

1

u/MetalInMyVeins111 Oct 06 '26

How much of it is AI?

1

u/Healthy_Ship4930 Oct 07 '26

I built everything. You don't have to take my word for it look at my GitHub history.

3

u/Slow-Ad9462 Oct 05 '26

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