← Devlog

Why we froze the VM

NAL has had two ways to run a game. The interpreter walks the parsed code directly, and it's what every game actually runs on. The bytecode VM lowers that code to a simpler intermediate form first. We started the VM in January for three reasons: sub-second hot reload, a step debugger, and speed. In September we built a harness that runs both side by side and compares the whole game state every frame (more on that here), and since then we've been closing the gaps so more code could run on the VM.

On 13 November we stopped. The VM is now frozen. Here's why.

Getting a real game onto it

By the end of October every game on the tech-demo shelf ran all of its systems on the VM. Those are small games, though. Wyrmworks, the colony-and-monster-hunting game we've been building this month, couldn't use the VM at all. The VM lowered user functions all or nothing, so the first helper function that touched an array sent the whole program back to the interpreter.

So we made each function lower on its own. A function the VM can't run becomes a stub, and anything that calls it falls back to the interpreter with a reason attached. Scalar constants and direct entity field access learned to lower too. That took Wyrmworks from 0 to 14 of its 55 systems on the VM, with 900 frames identical on both backends.

For the first time we could compare the two on a big game, with the same build and the same commit.

The measurement

Release build, Wyrmworks day 1InterpreterVM
Whole frame (3 rounds)2.11–2.13 ms2.18–2.20 ms
Simulation tick1051 µs1137 µs
The 14 systems the VM runs231 µs307 µs

The VM was slower, and not just overall. Every system paid a cost to enter the VM: systems that do almost nothing went from 0.1–0.9 µs a frame to 1.5–7.5 µs. Systems doing real work were slower too. PartFollow, which keeps a monster's breakable parts attached to it, went from 166 to 195 µs. ColonistAnim went from 17 to 30 µs.

We haven't profiled exactly why, so this is a hypothesis. A bytecode VM is good at arithmetic and control flow. NAL systems mostly don't do much of either. They're thin glue: read some fields, call a builtin, write some fields. On that kind of code the VM pays at the boundary, copying fields into its registers and back, converting values, and setting up each run.

What we decided

The other two reasons for the VM had gone away on their own. Hot reload now re-parses and runs on the interpreter, and nothing uses the debugger. We also wrote down when to reopen the question: if a profile shows NAL arithmetic or control flow (not field access or builtin calls) among the top costs, or if someone cuts the VM's entry cost and a new measurement puts it ahead.

Where the time actually went

With the VM off the table, we profiled the interpreter on Wyrmworks' move from season 1 to season 2. One item stood out: Rust's default hash function, SipHash, took 21% of the tick. Every NAL field read hashes a component name and a field name, and the interpreter hashes every local variable it looks up. SipHash is designed to resist deliberately crafted keys, but our keys are names from the game's own source, not untrusted input.

We swapped in a faster hasher with a fixed seed for those maps. Maps that leave the engine, like event data and MCP snapshots, kept the standard one. The results on Wyrmworks, release build:

Tick timeBeforeAfter
Season 11.177 ms0.785 ms
Season 21.364 ms0.915 ms

That's about a third off, and under the 1.1 ms target we'd set for the next milestone. It was a much smaller change than anything on the VM roadmap. As a side effect, the standard maps had been randomly seeded, so their iteration order changed from run to run. With a fixed seed it doesn't. Nothing depended on that order, but it's one less thing that could.

Earlier the same day we'd fixed a different slowdown. Tripling Wyrmworks' map added about 670 scenery entities, and every system, state machine and world query tested every one of them each frame. Frame time went over our 1.5 ms budget. A per-tick index from component, name and tag to entity IDs brought it back under, and debug builds check every indexed lookup against a full scan.

We'd planned the VM since January, and it took a real game to show it wasn't the fast path for code like ours. The harness that proved the two backends agree is also what made a fair speed comparison possible.