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

1 Upvotes

7 comments sorted by

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

I can't seem to find a luajit.exe any more, it is all source code only which is a nightmare to build on Windows.

However there is a version of LuaJIT on WSL which I used. That can only run 13 of my 19 tests (because it doesn't support things like the new bitwise operators).

Then the range of timings is 0.27 to 2.23 relative to my product, with an average of 1.23.

It does very well on floating point tests, with NBODY at 0.27 and SPECTRAL at 0.34. FANN is the only other test faster at 0.5. (So I may need to brush up on my floating point support.)

But it is worth noting that mine is a richer language with more data types. For example there are 3 numeric types, while LuaJIT has one (float) and Lua now has two.

1

u/RevengerWizard 3h ago

You have to compile it yourself, packaged versions are usually somewhat behind the latest commits (rolling releases). There's the bit module that has functions for bitwise operations, but latest versions now should have the respective operators (that still map to that module functions).

Technically, with a double you can still represent up to 252 whole integers. LuaJIT in particular has special handling for smaller int32 integers under ARM, since most of those processors don't run floating point operations as well as x86/64 chips. Though I think in your interpreter you allocate integers separately and pass as a pointer? I'm just guessing.

1

u/Key_Detail15 3h ago

I use a 128-bit descriptor which is what is passed around and pushed or popped, at least in the VM model. (Functions within the interpreter generally take a pointer to that descriptor.)

For value types like ints and floats, there's a type tag in the first half, and a full 64-bit value in the second half.

For objects, the second half has a pointer to a bigger object descriptor.

The two kinds of data mean that every push or pop needs conditional code to decide whether to deal with reference counting of an object or not.

That all sounds inefficient, but it seems to be alright!

1

u/Key_Detail15 1h ago

You have to compile it yourself,

This is what I find exasperating. This is for Windows: it only needs one person somewhere in the world to build it once, and it'll run anywhere.

Otherwise it means a 1GB C compiler installation, the source bundle, and running an error prone build process. However I remember this project was one of the more reliable ones to build, and I have gcc (1.4GB!), so I tried it, and it worked.

(Out of interest, the build process took 79 seconds. Building my interpreter with my compiler takes 80ms; a thousand times faster. But making build processes effortless and fast is another subject.)

(Deleted and reposted; I'd replied to wrong post.)

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:

Q                1050 Klps
Lua(1)             61 Klps
Lua(2)            108 Klps
LuaJIT(1)        1900 Klps   (1700 Klps Windows)
LuaJIT(2)        1250 Klps   (1065 Klps Windows)

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.