r/Compilers • • 19m 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.42

Q 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.

0 Upvotes

0 comments sorted by