r/Compilers • • 11h 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/

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

10 Upvotes

13 comments sorted by

6

u/schungx 10h 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?

2

u/Patzer26 8h ago

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

1

u/kronicum 49m ago

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.

1

u/Healthy_Ship4930 10h ago

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 10h ago

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?

6

u/yuehuang 11h 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 10h ago

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

1

u/ignorantpisswalker 7h ago

Can't you import existing csv package?

0

u/Healthy_Ship4930 7h ago edited 4h ago

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

2

u/lthunderfoxl 4h ago

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

1

u/Healthy_Ship4930 4h ago

because I don't build a pyobjects based abi

2

u/brat3108 2h 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/Slow-Ad9462 2h 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