This project is online and available for use, but I'm only doing basic maintenance.
Tech Used
AI Usage
Claude made improvements to my previous implementation and built out the benchmark and test suites.
Throughout many of my personal projects (like Retrograde), I kept writing the same virtual machine. Rather than having to maintain a separate VM for each project, I decided to pull it out into its own library.
There are plenty of great options for embedding a scripting language in a game, like Lua or wasm3. But they didn’t quite fit what I was after:
Most are built around a specific language. I wanted something any compiler could target, including my own.
Many bring their own garbage collector or runtime, which is overkill if all you want is to run some fast bytecode.
Few of them are designed around the kinds of things games do constantly, like calling back into the engine or crunching vector math.
So I ripped out the VM I had started for Retrograde, cleaned it up1, and made it into an amalgamated single-header library called Vroom.
A virtual machine is a program that pretends to be a CPU. Instead of running real machine code, it runs “bytecode”2, which is a made-up instruction set that’s designed to be easy to run in software.
The nice thing about this is that the same bytecode runs anywhere the VM does. You can ship it with your game, load it as a mod, or swap it out while the game is running, all without recompiling the engine.
The tradeoff is speed. Pretending to be a CPU is always going to be slower than being one, so the challenge here is making that gap as small as possible.
My goal was a VM that’s small enough to read through in an afternoon, and that does one job well. Because of this, Vroom doesn’t do some of the more advanced things other runtimes might, like optimize your code (that’s the compiler’s job). It loads the bytecode, checks it once to make sure it can’t do anything too bad, and then runs it as fast as it can.
Out of the box, Vroom doesn’t have a JIT compiler, which would convert the bytecode into real machine code while your game is running. That would make it faster, but also much more complex and harder to embed in a game. A JIT also makes it harder to run on platforms that don’t allow self-modifying code, like iOS. So by default, Vroom is entirely interpreted3.
This is just the runtime, the part of Vroom that runs bytecode inside your game. Producing that bytecode is up to your compiler, which can use the separate builder API we also provide.
Embedding Vroom just requires you to download the vroom.h header from the docs site, and include it in your project. In exactly one of your .c files, define VRM_IMPLEMENTATION before including it. That’s what pulls in the actual implementation.
#define VRM_IMPLEMENTATION#include "vroom.h"
From there, you only need to do three things:
Write a host call function. Whenever the bytecode needs something from your game (drawing, input, audio, etc), Vroom calls on_vrm_host_call(). Calls are identified by a number rather than a name, since looking things up by name is slower and games make a lot of these calls.
Create a VM and load a module. This is where Vroom checks the bytecode, once, before anything runs.
Call an exported function. Exports are also looked up by number. You can pass in arguments and get a result back.
// 1. Your game or app's handler for hostcalls (Vroom's syscall equivalent);// this is linked as an extern by the library, rather than being a function// pointer you assign.vrm_i64 on_vrm_host_call(vrm_VM *vm, vrm_i32 idx, vrm_Slot *win, vrm_i32 n) { switch (idx) { case 0: printf("%lld\n", (long long)win[0].i); return 0; } return -1;}// 2. Create the VM and load (and verify) the bytecodevrm_VM *vm = vrm_new(NULL);if (vrm_load(vm, bytes, length) != VRM_OK) { // the module failed verification}// 3. Call export 0 with a single argumentvrm_Slot args[1] = {{ .i = 100 }}, ret;vrm_call(vm, vrm_export(vm, 0), args, 1, &ret, NULL);vrm_free(vm);
Vroom is a “register” VM4, so its instructions read a lot like real assembly. Here’s the classic recursive Fibonacci function, written in Vroom’s text format to demonstrate the instruction set.
Every instruction is exactly 32 bits, and there are 162 of them in total5. The full instruction set is also published as a JSON file, so anyone can build their own tools against it.
At the moment, it really depends on the platform and the kind of you’re running. If you’re on my M4 Max, then yes, Vroom is pretty fast.
Here’s how it compares to wasm3, another fast interpreter, with plain C as a reference for how fast the same code runs natively. Lower is better.
Benchmark (ns/op)
Vroom
wasm3
C -O2
loop
2.29
2.29
0.22
call
7.02
7.24
0.67
fib
8.12
8.07
0.52
hostcall
3.13
4.39
0.67
float
2.24
2.30
0.77
branch
6.64
4.41
0.70
indirect
14.90
8.47
0.70
Heads up!
I keep updated benchmark numbers on the doc site, so if you’re reading this in the future, check there for the latest numbers.
It’s neck and neck with wasm3 on straight-line code, ahead on calls back into the host, and behind on branches and indirect calls. Host calls are something games do constantly, so I’m happy with where the tradeoffs have landed so far.
Vroom runs instructions using a trick called computed goto, where each instruction jumps straight to the code for the next one. It’s one of the classic ways to make an interpreter fast.
At some point, it quietly stopped being a computed goto loop. The compiler had noticed that all ~200 of those jumps looked the same, and helpfully merged them into one.
That empty asm statement with __COUNTER__ makes every copy look unique, so the compiler leaves them alone. Fixing that alone made things 10-17% faster across the board.
Since Vroom was originally designed for my Retrograde project, which targets the Raspberry Pi (Zero 2 W), I needed to make sure it ran well on that platform too. The Pi has a much simpler CPU than my laptop, and the interpreter was running 20 to 70 times slower than native C code.
To close that gap, I added an optional translator (VRM_WITH_A64) that converts whole functions into real arm64 machine code. It works a bit differently from a typical JIT though. Rather than watching the game run and compiling the hot spots, it translates everything once, right after the bytecode is loaded, and the result can be cached and reused on later runs. It’s switched off by default, so games that don’t need it never pay for it6.
After that, the interpreter is only 3.3x slower than native C code on a Raspberry Pi 3B+, which is a lot more reasonable.
Due to some potential internal changes in the direction Retrograde (and my other projects) are taking, my usage of VMs and Vroom are actually starting to shrink. I’m not sure how to make the library faster than it already is at this point7.
There’s also nothing currently using Vroom as the target backend, other than the Retrograde SDK (which is very much just a prototype). One potential idea is to eventually add Vroom support to Esper, which would allow any Esper frontend to potentially target it.