Skip to content

netcode: one message per link per frame, and lighter ones - #241

Merged
SandroMaglione merged 3 commits into
mainfrom
claude/project-thread-5neun0
Nov 21, 2026
Merged

SandroMaglione merged 3 commits into
mainfrom
claude/project-thread-5neun0

Conversation

@SandroMaglione

@SandroMaglione SandroMaglione commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Requested by Sandro · project thread

Why

A session handed its links one message for each thing it said (inputs, checksums, pings, pongs), on every step. A frame that ran several steps sent several of each, so a relay billed per message (a Cloudflare Durable Object) paid for all of them, and every game had to write its own outbox (Tabletop Champions did, in src/host/online/rooms.ts).

What changes

  • One bundle per link per frame. broadcast now puts a message in an outbox, and the outbox is sent once per frame. When session.update drives the session, a frame ends after the loop's last step. Each advance called directly counts as its own frame. Within one frame, a later inputs or hello replaces the earlier one, since it says everything the earlier one did.
  • FixedLoop tells update whether it is the frame's last step. update(stepSeconds, at: FixedLoop.StepInFrame) has at.last, worked out from the loop's own condition (accumulator and maxStepsPerFrame). It reuses two constant objects, so nothing is allocated per step.
  • Leaner encoding, protocol 5. from, v and epoch are written once per bundle. A message names its own epoch only when it differs (a save). Inputs are written as d (each distinct value once) plus i (an index per tick) or r (pairs of index and run length), whichever is shorter, so a held input costs two numbers. Acknowledgements are written as how far behind next they are. A ping's at is rounded to whole milliseconds. Every input a peer has not acknowledged is still carried, so robustness on a lossy link is unchanged.
  • Copies against loss now go out across frames. Reports of a seat gone and roster changes go out in the next five frames' bundles, instead of five times in one bundle (which one lost packet would take out together). A page that hides still sends its announcement five times at once, as five separate messages, because no frame follows.
  • status().sent reports { messages, bytes }: messages handed to the links, and their UTF-8 bytes.

Numbers

Measured with test/traffic.ts: pages driven by FixedLoop, each on its own frame times (85% at 60 Hz ± 2 ms, 10% at 33 ms, 5% at 50 ms, so some frames run 2 or 3 steps). The link is a MemoryNetworkLink with 120 ms latency, up to 60 ms jitter and 5% loss, and each run is 30 s. The rows are totals for all peers, as the room receives them. "Changing input" is the test game's pressed (a new input almost every tick). "Held input" changes every 20 frames.

before after (changing input) after (held input)
2 peers, messages/s 139 97 97
2 peers, KiB/s 62.0 22.8 15.5
8 peers, messages/s 728 387 387
8 peers, KiB/s 290 103 73
most messages one page sent in one frame 8 1 1

Tests

  • test/Traffic.test.ts (new) fails on main and passes here. It checks, for 2 and 8 peers, at most one message per link per frame with no halts, checksums verified and ticks together. It also checks a byte budget and that status().sent matches what the link saw.
  • A FixedLoop test for at.last, including the maxStepsPerFrame cap and frames with no step, plus type tests for StepInFrame, session.update's two arguments and status().sent.
  • Tests that wrote wire messages by hand now use a wire(...) helper for protocol 5.
  • The desync test now plays 10 more frames before checking that both peers are stopped. With fewer link sends, the seeded jitter differs, and the guest now spots the mismatch two frames before the host.
  • Determinism, rollback, resync, leave, hidden and soak tests are unchanged and pass. pnpm check passes, and pnpm --dir website build passes (the rollback demo forwards the loop's arguments).

Bench

The first push allocated 11% to 26% more per frame than the base. The extra came from template-string ropes that the UTF-8 count flattened, numbers turned into strings one at a time, scratch arrays emptied with length = 0 (which drops their storage), and copies made on parse. The third commit writes each bundle with one native JSON.stringify, compares inputs in place, and reads acknowledgements and indices in place. Measured against main on one machine: rollback-frame-2-peers allocates 6.6% more and runs 5% slower (within noise), and lockstep-frame-2-peers allocates 1.8% more. baseline.json is unchanged.

Breaking changes

These are in the changeset, with the fix:

  • Protocol 5: every page of a match must run this version (an older peer ends with ProtocolMismatch).
  • The function session.update(...) returns takes the loop's two arguments. A game whose own update wraps it passes them through: update: (seconds, at) => tick(seconds, at).

🤖 Generated with Claude Code

https://claude.ai/code/session_01HendNe9iA9HzfbsdcCgph5

Code that acts once per frame after its steps (a networked session that
sends what the frame said in one message) needs to know which update ends
the frame. The loop already decides it from the accumulator and
maxStepsPerFrame; it now passes it as update's second argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HendNe9iA9HzfbsdcCgph5
A session handed its links a message for each thing it said, every step,
so a frame that ran several steps sent several of each: a relay billed per
message (a Durable Object) paid for all of them, and every game had to
write its own outbox. Now a frame's messages go out as one bundle per
link, after the frame's last step when session.update drives the session.

- A bundle names the protocol, sender, and epoch once; inputs are written
  as the distinct values and which each tick had (a held input is one
  pair), acknowledgements as how far behind the sender's tick. Every
  unacknowledged input is still carried, so loss costs as before.
- Copies against loss (reports, roster changes) go out with the next five
  frames instead of five times in one; a hide still sends five at once.
- status().sent counts messages and bytes. Protocol version 5.
- Tests: at most one message per link per frame over a slow lossy link
  with multi-step frames (2 and 8 peers), a byte budget, and the counter.
  The lockstep bench's allocations per frame rise from 25.7 to 32.5 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HendNe9iA9HzfbsdcCgph5
@SandroMaglione SandroMaglione self-assigned this Nov 21, 2026
The bench gate compares allocations with the base commit, and bundles
cost 26% more per lockstep frame: template-string concatenation built a
rope that the UTF-8 count then flattened, every number became its own
string, scratch arrays emptied with length = 0 lost their storage, and
parsing copied the inputs and acknowledgements into new arrays.

Now each bundle is one native JSON.stringify of plain objects (members
left undefined are not written), inputs are told apart by comparing the
values in place, and the parsed message list, acknowledgements, and
indices are read in place. The wire format is unchanged. Against the base
on one machine: rollback frame +6.6%, lockstep +1.8% allocated per frame,
so the baseline is back to main's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HendNe9iA9HzfbsdcCgph5
@SandroMaglione
SandroMaglione merged commit 65f29c2 into main Nov 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants