One Rust engine for the servers and the browser
16 to 23 Nov 2026
- Since 22 Nov every game runs on one engine written in Rust: natively on the game servers and as WebAssembly in the browser. The TypeScript room, bots and physics glue are deleted: 348 files and 103,589 lines with their tests.
- On one benchmark with the same key presses on both sides, a room of 8 people cost 0.071 ms of CPU a tick on the TypeScript room and 0.020 ms on the Rust room. A room of 1 person and 7 bots cost 0.219 ms against 0.063 ms.
- 12 bot rooms in one process used 317 MB of memory on the TypeScript room and 29 MB on the Rust room, which has no garbage collector.
- In a load test with real network clients on one core, the old server kept 32 bot rooms on time and the Rust server 192.
- A one-line change to a game mode rebuilds in 3 to 9 s instead of about 20 s, and a release build after a bot change takes 126 s instead of 264 s.
Every rule is now written once and runs the same on the server and in the page, so a change is made and proven once instead of twice.
TagBalls started as one TypeScript program. The same code was the game room on the servers (on Node), the predictor in the browser (the page runs the game a little ahead of the server so your ball answers your keys at once), local games, the editor’s play test and the bots. Physics was Box2D, a C library, compiled to WebAssembly for both. Between 16 and 22 Nov we replaced the TypeScript with one engine written in Rust. The physics is still Box2D: the same C library, now built into the Rust engine. This post covers why, the order things moved in, how we proved it plays the same game, and the numbers. The wiki page Rust game rooms describes the system as it is now.
Why we moved
On a one-core game server the Node room’s limit was never average CPU. It was single long pauses on the event loop that every game on the process shares: garbage collection, waiting to send, and the bots’ thinking. When one tick ran long, every game on that process was late.
On 16 Nov we wrote a game room in Rust as a prototype, to see what a room costs without a garbage collector. It builds Box2D natively with the settings that give the WebAssembly build’s exact bits: no fused multiply-add, the sine, cosine and arctangent from the same C library sources the WebAssembly build links, and on arm64 the min and max written as on x86_64, because they differ for -0 and +0. On our one-core US West server, with 12 rooms of people, its slowest 1% of ticks took 3.1 ms of CPU against Node’s 4.8 ms, and its process used 34 to 37 MB against 258 to 290 MB. From 18 Nov public Capture games moved to it.
That left two engines. Every rule (a power-up, a map piece, a mode, a bot’s choice) had to be written in TypeScript for the browser and in Rust for the servers, and proven to give the same game. A rule changed in one and not the other showed up as a ball the browser drew in one place and the server put in another. So we moved everything to the Rust engine and compiled it to WebAssembly for the page.
What moved, in order
| Date | What moved |
|---|
| 16 to 17 Nov | The Rust room prototype, then measured on the live US West server while nobody played there |
| 18 Nov | New public Capture games in all 4 regions with a Dedicated Server, then ranked games and gathered bot games |
| 19 to 20 Nov | In one night the feature list went from 140 to 154 names and US West ran all 8 public game lines on Rust. Local games, the editor’s play test, the Sandbox and the tutorial started running the Rust room in the page |
| 20 to 22 Nov | The page’s predictor moved onto the room’s own code in the WebAssembly module |
| 22 Nov | The Node room host, then the TypeScript room, modes, mini-games and bots, then the page’s TypeScript physics world were deleted |
Public games ran on the servers the whole time. We put an annotated git tag on the last commit before each big change, 44 tags across the port, so any step can be checked out and run again. Every "before" number below comes from the last tag that still had the TypeScript engine.
How we kept live games working
- One feature at a time. Each Rust server reports a list of the features it can play (177 on 22 Nov). The site sent a game to a Rust room only when a server reported every feature that game needs, and otherwise to the TypeScript room. A missing feature meant the old room, never a broken game.
- A separate checker. One agent checked each set of engine changes (build, tests, recorded games, server checks) and only said pass or fail. It never wrote fixes. Nothing reached the game servers until it passed.
- The browser last. Page bots, local games, the editor’s play test and the predictor moved to the WebAssembly module one at a time, each with its own switch, before anything was deleted.
- A failure in one room closes only that room. The first Rust build stopped the whole process on any internal error, and an audit found four outside inputs that could reach one. We fixed those, then caught failures around each room’s step, message and join. In a test with four rooms, the fourth made to fail three ways, the other three kept identical snapshots tick for tick. It costs about 3.5% more CPU per room tick.
How we proved it is the same game
Same means the same bytes. A recorder kept the inputs of a game and every byte the TypeScript room sent. The Rust room played the same inputs and had to send the same bytes at every tick. On 18 Nov all 284 recorded games matched. The bots were held to the same bar: the Rust bot brain had to press exactly the keys the TypeScript bots pressed, on 8,706 recorded bot games, and on 1,600 of 1,609 reference games made for the bot situations a survey of the maps found (the other 9 were not played).
- Every check ran with a planted fault that had to make it fail. For example, moving the cannon’s core half a tile made 178 of 178 cannon games differ.
- Recordings only show what they recorded, so a tool re-recorded every game on the current TypeScript room after each change to it. Its first run found one missed change.
- Recordings added players straight into the room, so they never tested the door. A check now joins both rooms with real join tickets and compares the player list field by field, and compares what each player receives, not only what the room sends. That found a player holding Tagball, Rolling Bomb or Freeze Bomb being sent where enemies would respawn.
- Before the delete, the last TypeScript answers were frozen. The 4,031 recorded games the checks run now hold the Rust room’s own answers from 22 Nov, so a change today is compared with the Rust room before it.
Some things differ on purpose. Each bot brain draws its own random numbers, so in a benchmark the bots move differently on the two engines. In a benchmark with 24 rooms, the TypeScript rooms shared one seeded random source and the Rust rooms have one each. The Rust room also sends more of the small JSON messages (scores, events, player lists), which we have not broken down yet.
Benchmarks
Measured how: on our build laptop (Intel Core Ultra 9 275HX, Linux in WSL2, Node 22.23.0), 22 Nov, at a 1-minute load of 0.9 to 2.8. Each benchmark runs its rooms in one process, every room stepped in turn: 600 ticks to warm up, then 30 s of game time at 60 ticks a second, with every person’s snapshots sent over a real local socket. The people press keys from one script with the same seed on both sides, so each person presses the same key at the same tick in both rooms. Map tp-asida, teams of 4, Expert bots. TypeScript then Rust, 3 runs each in turn; the median is shown.
| Per person, a second | TypeScript | Rust |
|---|
| Snapshot bytes, 1 room of 8 people | 3,043 | 3,043 |
| Snapshot bytes, 1 room of 1 person and 7 bots | 2,951 | 3,383 |
| Garbage collections a minute, 12 bot rooms | 432 | none |
With 8 people the snapshots came to the same bytes a second on both rooms. The two benchmarks hash different things, so this is not proven tick by tick. Bot games differ because each brain draws its own random numbers.
How many rooms one core keeps on time: a tick is 16.7 ms at 60 ticks a second, so a core keeps up while the slowest 1% of its ticks stay under that. With rooms of 8 people, one core of the laptop kept between 128 and 160 rooms on the TypeScript room and between 192 and 256 on the Rust room. With bot rooms it was between 32 and 64 against between 128 and 192 (from the first benchmarks, which used each engine’s own key script). The TypeScript times here are wall clock, because Node’s thread CPU clock counts in steps of 4 ms on this machine; over whole runs the two clocks agreed within 1%.
Rooms of people gained less than the average CPU suggests. At 192 rooms the Rust room’s slowest ticks were 2.3 times its average. All of them were tick 59 of each second: the benchmark makes every room at the same moment, so every room ran its once-a-second work (the scoreboard and the rules’ checks for idle players, votes and held seats) on the same tick. A server makes rooms when people ask for them, so about one room in 60 does that work on any tick. With the rooms started at different ticks, the slowest 1% at 192 rooms fell from about 18 ms to about 13.5 ms on the Mac (Apple M3 Max, two runs each). That gain is in the measurement, not in the code.
Past about 64 rooms in one process the Rust room’s memory grew faster than its rooms: 617 MB at 192 rooms and 3,640 MB at 512 on the Mac. The physics library numbers every body and shape in one table for the whole process, and each room kept its own list indexed by those numbers, so room number i held an empty slot for every shape of the rooms made before it. The lists are now kept in pages of 64 slots, made when first written. 512 rooms went to 402 MB and 192 rooms to 163 MB, with the same benchmark results. Our servers ran far fewer rooms a process than that, so no player saw it.
Under real network load
The benchmarks have no network. The load test opens real WebSocket connections with real join tickets, sends keys from a seeded script at the page’s rate and pings every 250 ms. The server gets one core of the build laptop to itself, each step runs 120 s after 20 s of warm-up, and a step passes when 99 in 100 ticks start within 16.7 ms of schedule and no connection drops.
| One core, real clients | Node room host | Rust room |
|---|
| Rooms of 8 people kept on time | 128, failed at 192 | 192, degraded at 256 |
| Bot rooms (1 person, 7 bots) kept on time | 32, failed at 64 | 192, failed at 256 |
| At 128 rooms of 8 people: CPU of the core | 66% | 40% |
| At 128 rooms of 8 people: memory | 603 MB | 356 MB |
| At 128 rooms of 8 people: ticks late, p99 | 11.0 ms | 1.8 ms |
Build laptop, 22 Nov. The rows that decided the result ran twice and agreed; every bot row ran once. Both servers sent about 3.6 KB a second to each person. The memory numbers are from before the memory fix above.
On a live server: on 23 Nov we ran up to 24 bot rooms on the US East server through the site’s real join path. Each added room cost 1.19% of the core and 0.21 ms of CPU a room-tick, and about 1.05 MB. The slowest 1% of ticks started 3.5 to 4.2 ms late from the first rooms up, against about 1 ms on the build of 22 Nov. It does not grow with load, and we do not know the cause yet.
In the browser
- A game now loads one WebAssembly module, the Rust room (7,139,962 bytes as built on 22 Nov, 1,833,323 with brotli). Before, every game also waited for the page’s own Box2D module (332,450 bytes).
- The bot brain is a second module (3,518,832 bytes, 999,776 with brotli) for bots on the page.
- All the site’s JavaScript files together went from 22,413,741 bytes (5,225,820 with brotli) on 21 Nov to 14,805,119 bytes (3,320,700). That is every file the site can load, not what one page downloads.
- The predicted frame in Chrome was not measured on both engines. Our browser benchmark’s online scene never started counting frames against the local Rust server, and the Mac was too busy for timings that afternoon.
Build and test speed
After the delete every rule change is a Rust change. A TypeScript edit showed its result in seconds. A Rust edit took 5 to 30 s to rebuild for a check, and a release build 4 to 5 minutes. Up to 20 agents build at once, each in its own git worktree, so the first build of every worktree counted too.
We timed three kinds of one-line edit with rustc’s own pass timer. An edit to a game mode took 29.2 s on the laptop, 23.9 s of it in LLVM. rustc puts every method of the room type in the codegen unit (the piece of a crate LLVM compiles as one) of the module that defines the type, whichever file the method is written in. That unit held 12,814 of the crate’s 85,370 compiled items, so any edit to any game mode sent all of it through LLVM again on one thread.
- One command from an edit to its result (
rs-loop): it builds only what changed, runs one test, replay or benchmark, and prints both times. - New worktrees start from a shared build of main. rustc throws away an incremental cache made in another folder because it records the working folder, so our crates build through a small wrapper that records it as ".". The binary plays the same either way.
- The server became three crates: the room, the bot brain and the server. A change to the brain or the server no longer compiles the room. A script writes the split from the code in under a minute, and no file moved.
- Each game mode’s methods compile in a codegen unit of their own in the fast build. A small macro, added by script as one attribute line per block, moves each method’s body into a function in its own module. Its tokens carry the attribute’s source position, so an edit inside a body no longer changes the method left in the room’s unit. Release builds compile the code as written.
- The two browser modules now go through
wasm-opt -O3 after rustc: the room’s from 7,981,821 to 7,094,485 bytes and the brain’s from 3,925,081 to 3,497,893, with identical outputs on the recorded call logs. Chrome refuses to compile a module over 8,388,608 bytes on the main thread, and the room’s was close.
Every step was checked against the recorded games: all 4,031 played the same after the crate split and with the game mode units, the benchmark hashes did not change, and the time per room tick stayed within the spread between runs. The server binary’s code is 2.2% larger after the split, because some calls now cross crates.
What we deleted
| Before | After |
|---|
| TypeScript engine (room, modes, mini-games, bots) | 49,074 lines in 113 files | deleted |
| Tests, benchmarks and ladders that ran it | 54,515 lines in 235 files | deleted |
| Test and browser test scripts | 1,233 | 1,118 |
| Type check of the whole repo, from cold | 4.49 s | 3.88 s |
Build laptop, 3 runs each, median. The Rust code is 159,498 lines: the room and its server 100,833 (including the network layer, which sat outside the engine on Node), the bot brain 52,327, and 6,338 for the browser modules’ glue and tests.
What went wrong on the way
- On the night of 19 to 20 Nov one set of changes broke the Rust bots’ keys on some maps. Two changes each read the map’s free power-up orbs and drew a random number for each, so after the merge each orb drew twice and every later random number was one off. Each change was identical on its own recordings; only the merged tree was wrong. The checker caught it before the game servers ran it, and the fix shipped 38 minutes later. Since then a set of changes that turns something on for players is checked before the site deploy as well.
- Kept answers recorded on the wrong machine. The predictor’s kept answers were recorded and proven on the build laptop (x86_64), and the deploy checks run on the Mac (arm64). 15 of 543 games failed there. Node 22 answers
Math.exp one last bit apart on the two processors for about 1 argument in 1,500, and the page eases the drawn place of another ball with it. Each machine now keeps its own answers for the entries that differ, so both are checked exactly with no tolerance. Players never saw it: Chrome already differs from Node in that last digit on about 1 argument in 10. - Stale recordings. 38 recordings of layered gravity maps differed by one bit on a merged tree. They had been made before a wall jump change. Looking into it also found a bug in the compare tool, which read a switch set to 0 as on.
- A private game sitting in its lobby during a server restart came back open, with no password. The password now lives with the game’s code on the site, not only in the room’s memory.
- The bot brain never freed its own physics worlds, and a game server never reused a closed room’s slot. Both are in Fixes, 22 to 23 November.
What did not help
- A faster linker: linking was 0.26 to 0.35 s of a rebuild.
- A faster test runner: the whole Rust test suite runs in 3.3 to 4.5 s. The time is in building the test binary.
- More codegen units, opt-level 1 or 2, and no MIR inlining each left the game mode rebuild at 24 to 26 s. Opt-level 0 took 6.3 s, but tests then ran 2.7 times slower.
- Moving a mode into a trait, a closure or a nested function: rustc still rebuilt the big unit, because the method left behind had a source position covering the body.
wasm-opt -Os and -Oz saved only 5 to 41 KB more than -O3, and -O4 made the module bigger.
Still owed
- The cause of the live late-tick floor above.
- The load test after the memory fix, and a full ramp on a live server. A server takes at most 24 connections from one address, so a ramp from one test machine stops at 3 rooms of people.
- Top-down mini-games on a gravity map never had bots in TypeScript either, and still have none.
- A server restart keeps the score, clock and places, but not each ball’s spin or the bots’ memory.
The current system is in the wiki: Rust game rooms, Load tests and Server settings and capacity.