Repository navigation
netcode: one message per link per frame, and lighter ones - #241
Merged
Merged
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
broadcastnow puts a message in an outbox, and the outbox is sent once per frame. Whensession.updatedrives the session, a frame ends after the loop's last step. Eachadvancecalled directly counts as its own frame. Within one frame, a laterinputsorhelloreplaces the earlier one, since it says everything the earlier one did.FixedLooptellsupdatewhether it is the frame's last step.update(stepSeconds, at: FixedLoop.StepInFrame)hasat.last, worked out from the loop's own condition (accumulator andmaxStepsPerFrame). It reuses two constant objects, so nothing is allocated per step.from,vandepochare written once per bundle. A message names its own epoch only when it differs (a save). Inputs are written asd(each distinct value once) plusi(an index per tick) orr(pairs of index and run length), whichever is shorter, so a held input costs two numbers. Acknowledgements are written as how far behindnextthey are. A ping'satis rounded to whole milliseconds. Every input a peer has not acknowledged is still carried, so robustness on a lossy link is unchanged.status().sentreports{ messages, bytes }: messages handed to the links, and their UTF-8 bytes.Numbers
Measured with
test/traffic.ts: pages driven byFixedLoop, 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 aMemoryNetworkLinkwith 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'spressed(a new input almost every tick). "Held input" changes every 20 frames.Tests
test/Traffic.test.ts(new) fails onmainand 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 thatstatus().sentmatches what the link saw.FixedLooptest forat.last, including themaxStepsPerFramecap and frames with no step, plus type tests forStepInFrame,session.update's two arguments andstatus().sent.wire(...)helper for protocol 5.pnpm checkpasses, andpnpm --dir website buildpasses (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 nativeJSON.stringify, compares inputs in place, and reads acknowledgements and indices in place. Measured against main on one machine:rollback-frame-2-peersallocates 6.6% more and runs 5% slower (within noise), andlockstep-frame-2-peersallocates 1.8% more.baseline.jsonis unchanged.Breaking changes
These are in the changeset, with the fix:
ProtocolMismatch).session.update(...)returns takes the loop's two arguments. A game whose ownupdatewraps it passes them through:update: (seconds, at) => tick(seconds, at).🤖 Generated with Claude Code
https://claude.ai/code/session_01HendNe9iA9HzfbsdcCgph5