Kelyje II · faster · week of
The browser build now takes 18.56% less time per frame
Code translated by machine from a binary must keep every virtual call the original made. Once the game is back in C++, a devirtualising pass can turn most of those into direct calls: here, 96.2% of them, and the browser build takes 18.56% less time per frame.
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; the C++ rewrite is the same game as code a person can read · kelyje2.lt
The C++ rewrite, now verified against the original, can replace the translated code where the game spends its time. I wanted the rewrite for reading first; speed was the plan once I knew it matched.
One slice at a time
I compile the rewrite to WebAssembly in slices (groups of related functions) and swap each into the busiest parts of the browser build. Each slice keeps the old code behind it: if a call arrives in a case the new code has not been proved to handle, the old code handles it instead, and I count each such fallback. The first slice was a trial, rotation maths: 1,046,942 calls went through it and none fell back. Next came a bridge so old and new code could call each other, then three slices for the scene: query, walk and render.
Virtual calls made direct
A virtual call looks up its target in a table while the game runs. A direct call already knows its target, which costs less and lets the compiler inline it. A devirtualising pass in my build makes that change wherever the target is known.
With query and walk in, 94.8% of the 26.9 million calls that used to be virtual in the 1,000-frame test drive were direct. Render took that to 96.2%.
How I measured it
Each measurement runs old, new, new, old, so anything that drifts during the run affects both equally. It runs on a machine with nothing else busy, over frames 100 to 1,000 of the scripted drive used to verify the rewrite. A sample only counts if the build still reproduces all 1,000 reference frames bit for bit, so a faster build that is subtly wrong cannot win.
With query and walk only, the game's thread went from 20.876 to 17.088 ms per frame, 18.1% less. With render added and all three on by default, it is 18.56% less. That is the game's own thread; putting the picture on screen is separate and takes about 0.03 ms.
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