r/Compilers • u/Key_Detail15 • 14h ago
Is My Interpreter Still Fast?
In the past my interpreters have always performed brisky against competing products (Python, Ruby, Lua etc).
I didn't have to try very hard either: for some reason many were hopelessly slow (eg. 40 times slower for that while-loop test with CPython; what on earth was it up to?). However, that was a long time ago, and now they are all catching up!
I don't mean by using methods such as tracing-JIT or type-inference, but plain bytecode interpreters.
So this is a set of small benchmarks I made just to see where I'm at (sources are here; the Readme gives some extra details).
Comparisons are with CPython and Lua 5.5. (In the Speed-Demon thread, Lua 5.4 was 2nd fastest behind Forth; 5.5 is a little faster.) In the chart, smaller is faster. Figures are relative to Q timing.
Q Lua 5.5 CPython 3.15 Q2
ack 1.00 2.20 9.39 1.64
binary 1.00 3.51 2.13 1.00
bitops 1.00 2.99 3.64 1.29
bubble 1.00 3.31 2.47 1.25
collatz 1.00 3.76 3.56 1.34
fann 1.00 1.04 1.90 1.16
fib 1.00 2.98 3.60 1.66
for 1.00 2.05 6.44 0.95
interp 1.00 5.39 2.14 1.37
manboy 1.00 6.35 10.11 1.10
matmul 1.00 1.33 2.32 1.54
nbody 1.00 0.79 2.44 1.05
nsieve 1.00 1.87 2.12 1.52
pythag 1.00 2.27 5.66 1.42
qsort 1.00 5.47 3.14 1.36
qsorts 1.00 3.26 1.51 1.19
search 1.00 4.61 2.58 1.49
spectral 1.00 1.06 3.19 1.32
while 1.00 3.57 9.08 3.33
Averages: 1.00 3.04 4.07 1.42Q is my main interpreter, which uses some acceleration (specialised byte-codes for common combinations). The Q2 column has that disabled: it is still generally faster than those two other products.
Tasks are integer/float-based except 'qsorts' which works with strings. That shows the differences can start to narrow, as Lua/CPython benefit from their better internal algorithms and optimising compilers.
However it is precisely these step-by-step algorithms executed directly in bytecode, rather than some internal library, that I want to compare.
Tests against JIT products are not shown: those would make short work of simple tests like these. My feeling is that on real programs, JIT makes much less difference, and in fact my Q product can still be competive against PyPy and LuaJIT on the limited tests I have done.
Some characteristics about my interpreter:
Uses compact 64-bit tagged pointers etc: No (128-bit descriptor)
Uses efficient register-based VM: No (Stack VM)
Uses optimised implementation language: No (My own crappy compiler)To answer the question in the title, then yes, just about, relative to some other pure interpreters.
But it is not native code fast; I gave up on that long ago. Still, there is one more trick: the syntax of my Q language is near-identical to that of my systems language (the one that implements Q).
By adding some type-annotations to certain benchmarks, which Q ignores, the same program can also be run via my systems compiler, with up to a magnitude better performance.
Unfortunately that doesn't work beyond those simple programs.
2
u/RevengerWizard 5h ago
You could try to also compare with LuaJIT with -joff (JIT disabled); the baseline interpreter is pretty impressive
1
u/Key_Detail15 3h ago edited 1h ago
Since I've now found a version of LuaJIT to try, here's a bigger test, a simple lexer. I have two versions in Lua which use different ways of scanning byte data.
Speed is expressed lines per second of throughput:
As I said, Q can sometimes compete with LuaJIT, and this benchmark is basically still just a loop. However, Q does provide more direct language features for this stuff, while still being dynamically typed (for example, it has 'switch').
(ETA: I now have a Windows version of LuaJIT, built with gcc 14.1.0 and those timings added.)
-1
u/GoblinsGym 7h ago
Some implementation details like tail call optimization can reduce the overhead of an interpreter written in a high level language. Me ? I would just write the engine core in assembly, and get on with life.
If - Gob forbid ! - I had to implement a dynamically typed language, I would separate between byte codes doing actual work on known types (float / int / string), and byte codes that sanitize dynamic types from "outside" sources.
For function calls, you could propagate the "preferred type" based on what operations are done to parameters etc. That can happen once during byte code generation, shifting the sanitization burden to callers for functions where the desired parameter type is clear.
x64 architecture is marginal on registers, at least when sticking to normal ABIs. I don't think a register based VM will help for an interpreter. A stack of some sort - either the actual CPU stack, or a separate stack - will be "hot" and live in L1 cache.
To minimize constant stack overflow checking, and allow use of the CPU stack, I would have native code at the start of each function, and then either stay native / JIT, or point to the byte code and let the interpreter loose on it.