r/Compilers • u/Healthy_Ship4930 • Oct 17 '26
Function calls in Python compiler went from 893 to 85 instructions
Hey everyone :)
Here I am after 8 months on the roadmap of Edge Python, my own Python compiler.
I want to share a small optimization I implemented for all function and object calls.
Compilers usually inline leaf functions, where the theoretical best is around 80 VM instructions.
A normal call in Edge Python cost 893 and for CPython 300 instructions.
Working on this problem I got it down to 85, with the body running inline and no frame.
For recursion I used reversible computation. Each call applies a step like n -> n - 1 and each return undoes it with n -> n + 1, so no frames are needed. Applying the same pattern to the other paths keeps all of them below 90 instructions.
I also removed the slow path completely. It now works for try and any kind of objects or functions.
Just want to show this is not a new algorithm but all this months studying runtimes and building edge python I didn't see anything similar.
11
u/AustinVelonaut Oct 17 '26
I don't understand what you are measuring, here. Is this for a particular benchmark test, or are you saying that any function call requires 85 VM instructions? Is this a static (compiler generates 85 insns) or dynamic (call executes 85 VM insns)? Why so many?
Also, how can you have reversible computation for destructive operations like a store into an array?
20
u/Healthy_Ship4930 Oct 17 '26
Not AI slop btw, 9 months of work on this.
With so much AI where I work, spending weeks on a problem like this makes me feel useful again 🙂.
4
u/firemark_pl Oct 17 '26
Show me benchmarks!
Many opcodes are created because of speed up.
1
u/Competitive_Ideal866 Oct 18 '26
The ideal would be 1 instruction?
2
u/AromaticCredit1533 Oct 20 '26
Thanks for sharing!
I'm also building my own Python compiler too, called Formal Python. Your Edge Python compiles to WASM, while in my case I chose OCaml's bytecode as target because I want ecosystem interop with OCaml which is my main language (and it has native compilation too). I don't know if this is useful, but I wanna share just in case. My optimization methodology is to strip away some expressiveness / change the semantics of Python so as to trade for predictability, so that the compiler can make more assumptions. For example, the compiler expects lexical scoping instead of dynamic scoping. In a sense, this is a new sub-language of Python. This is similar to the route that PyPy took too, specifically the RPython language which is used to write PyPy. I know this will limit the kind of code I can compile. But also, so far I plan to only use Formal Python for my personal Python code, and also to compile the source code of JAX and friend libraries which are written in this style actually, so for me it's fine to make this trade :-)
1
u/Far-Championship-883 29d ago
SO did the work focus on reducing register spills ? more registers ? more efficiency ?> in-lining >? code folding ? link to diffs, generally curious I have been working on a AOT and it's not as fast as cython but I am currently working towards full language compliance first.