ovi labs

Kelyje II · faster · week of

Passing a struct by pointer kept 1,525 functions on the stack

Passing a small struct by address to an outlined helper forces the compiler to keep it in memory instead of registers. Switching our x86 lazy-flags helpers to pass fields by value cut the Wasm binary by 15.8% and took 9.1% off Firefox frame time, crossing 60 FPS; a second pass that skips dead flag flushes before calls took another 7.3% off the binary.

Kelyje II: the Lithuanian release of Rig'n'Roll, a truck game from 2000. The browser build is its x86 code translated to WebAssembly by machine · kelyje2.lt 

After merging triangle fans cut a quarter of the draws, the renderer was no longer holding Firefox back: three quarters of each frame was guest WebAssembly on the game worker. A profile showed one small helper, lf_get_mask, taking a large slice of that time.

One address-of forced every function onto the stack

Translated x86 code tracks CPU status flags (zero, sign, carry, overflow, parity, adjust) lazily in a LazyFlags struct, computing the bits only when a branch or call needs them. Clang at -O2 was outlining lf_get_mask and condition instead of inlining them. Because every call passed &lf by pointer, the compiler had to allocate LazyFlags in linear memory on the stack in every generated function.

In one translated source file alone (recomp_003.c), that meant 1,525 stack slots and 11,125 out-of-line flag calls, turning every flag update into six memory stores.

before (by pointer)
  lf_set(&lf, ...)  -> 6 stores to RAM
  lf_get_mask(&lf)  -> call + reload RAM

after (by value in Wasm locals)
  lf_set(...)       -> Wasm locals only
  condition(...)    -> inline compare

I changed wasm/shim.h to pass the LazyFlags fields by value, marked the small flag helpers always_inline, and kept one noinline fallback per mask class with a 32-bit shortcut for the rare cases that cannot fold at compile time. Nothing is dropped: every path computes the exact same flags as before.

In recomp_003.c, stack slots dropped from 1,525 to 0 and out-of-line flag calls from 11,125 to 936. Across the whole binary, game-native.wasm shrank from 24.42 to 20.57 MB (−15.8%). On a quiet alternating run over frames 100 to 1,000 of the 1,000-frame scripted drive, Edge dropped from 11.763 to 10.580 ms per frame (−10.1%) and Firefox 160.0.1 dropped from 17.864 to 16.230 ms (−9.1%, 61.6 FPS). An exhaustive check tested 8,967,296 flag states across 4.21 billion comparisons with zero differences.

Skipping flag flushes that no callee reads

With LazyFlags in Wasm locals, the next waste stood out. Before every lifted guest call, the generated code materialises all six status bits into c->eflags in case the callee reads them. In practice, almost all Win32 functions overwrite the status flags before reading them.

I added a static backward-liveness pass (tools/callflags) over all 12,669 compiled function bodies. Where a callee is proved to overwrite every status flag on all entry paths before any read (or only passes flags through to a caller that immediately overwrites them), the pre-call flush becomes a single store of the non-status bits. All 62 patch override addresses and native wrappers are excluded so nothing can be rebound out from under the proof.

That pass removed the flag calculation at 31,874 of 45,842 static call sites and 4,025 of 4,429 tail jumps (62.6% of executed guest calls per frame), taking another 1.50 MB (−7.3%) off the binary. In its own quiet alternating run against the locals build, Edge improved by 1.47% (9.731 to 9.588 ms per frame) and Firefox by 1.11% (15.441 to 15.271 ms, 65.5 FPS).

Proving it with a poison build

To verify that no hidden flag reader slipped past the static proof, I built a poison variant that writes the bitwise complement of the real status flags at every skipped call site. On the 1,000-frame scripted drive, both the normal build and the poison build matched the canonical state hash, pixel hash and draw count (193,366 draws, zero differing pixels), alongside 12,800 fixture runs and 415 graphics regression tests. That proves every executed path on the scripted drive; unexercised paths rely on the static proof and the 25-fixture test suite.

I'm Ovi. I recompile old Windows games to run in a browser, build a virtual GPU for Windows XP, and write tools for my own machines, with AI since 2020. Projects and the latest log