Skip to content

perf(rpc): serialize and parse each stream event once and use native base64 - #390

Open
Exeloo wants to merge 6 commits into
mainfrom
perf/rpc-codec
Open

Exeloo wants to merge 6 commits into
mainfrom
perf/rpc-codec

Conversation

@Exeloo

@Exeloo Exeloo commented Nov 29, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Closes #260
Closes #270

Codec-level performance fixes in @nanoforge-dev/editor-rpc, plus two small fixes found on the way. The wire text of stream events and the base64 text are unchanged; the one encoding change is a Node Buffer, which now goes out as bytes (below).

  • RPC stream events are serialized and parsed three times each #260, events serialized and parsed once: RpcSession._emit splices encode(event) into the message instead of encode, parse, stringify. RpcClient revives $bin/$date tags only when the message text holds one, so untagged events (game frames) are parsed once. An event that isn't a JSON value now throws a TypeError instead of a SyntaxError.
  • Base64-encoding readMany answers blocks the server for about 100 ms per 8 MiB #270, native base64 encode: encode uses Uint8Array.prototype.toBase64 when the runtime has it (Bun, Node, recent browsers), with the btoa loop as fallback. Buffer is not used because the codec also runs in the browser.
  • Native base64 decode: fromBase64 uses Uint8Array.fromBase64 when present, with the atob loop as fallback. Both accept and refuse the same texts; callers already turn a decode error into BAD_REQUEST or "Unexpected response".
  • Subscription ids: RpcSession.receive ignores sub/unsub messages whose id is not a safe integer, as it already ignores other broken messages. The editor's client only sends its integer counter.
  • Node Buffer: the replacer checks the raw value before toJSON runs, so a Buffer is sent as a $bin tag instead of { type: 'Buffer', data: [...] }.

How do you test this PR?

Before/after (ms per op, minimum of 5 runs; the bench calls the real RpcSession._emit, RpcClient._receive, encode and decode, built from each revision):

node before node after bun before bun after
client receive, 420 KB world event 6.76 1.52 7.10 1.34
server emit, 420 KB world event 3.21 1.93 4.22 2.80
client receive, 4 KB event 0.070 0.019 0.069 0.013
encode 8 MiB readMany answer 190 5.5 119 9.6
decode 8 MiB 14.9 5.5 10.4 5.1

The client side runs in the browser; node gives a V8 number, Chrome itself was not measured. Every bench run checked that the wire text and the base64 text match the old code.

Tests (packages/rpc/test):

  • Wire identity and tag revival for stream events (bytes, dates, a $bin look-alike, "$bin": inside a string, U+2028), base64 parity with btoa/atob, and both fallbacks with the native functions removed. These are equivalence guards: they pass on the old code too.
  • "ignores a subscription whose id is not an integer": before the handler was called 6 times (1 failed, 20 passed); after 21/21.
  • "encodes a Node Buffer as bytes": before {"content":{"type":"Buffer","data":[1,2]}}; after {"content":{"$bin":"AQI="}}, 22/22.

Commands run from the worktree on a8a1383: pnpm build (17/17), pnpm lint (29/29, knip included), pnpm typecheck, and the unit tests of every dependent package: code 48, project 63, runtime 13, settings 27, rpc 22, server-core 129, all passed, 0 skipped. Not run on this branch alone: packages/ui, the plugins, e2e.

Integration: a scratch branch with every editor branch of this sweep merged passed pnpm build, lint, typecheck, pnpm test --force (1014 passed, 1 skipped) and the full e2e suite with the engine (141/141, Chromium only).

Merge danger

  • Two-way door: no wire format change, a revert is enough.
  • Blast radius: the editor. @nanoforge-dev/editor-rpc is private, with 7 runtime dependents in the editor (code, project, protocol, runtime, server-core, settings, ui). hasTags is a new export of the package; the SDK re-exports only named symbols, so its public surface is unchanged. Merging to main deploys the editor app.
  • The id check refuses only messages the editor's own client never sends.

Follow-up issues

None filed. A binary response for read/readMany (suggested in #270) stays a possible next step.

Quality gate

Lane Result HEAD covered
Docs N/A: internal codec, wire unchanged 3a9bc03
Correctness Pass (Minor: a detached buffer throws, error class change: no impact) 3a9bc03
Performance Pass (producer bench, rerun by the reviewer) 3a9bc03
Accessibility N/A: no UI -
Security Pass 3a9bc03
Style Pass (Minor: blank line after the codec header, fixed in a8a1383) 3a9bc03

Last round 9eee610 (Buffer as bytes) and a8a1383 (style): producer red→green, not re-reviewed.

The session splices the encoded event into its message instead of parsing
it back and stringifying it again. The client revives tags only when the
message text holds one, so untagged events (game frames) are parsed once.
The wire text is unchanged.

Refs #260
The codec uses Uint8Array.prototype.toBase64 when the runtime has it (Bun,
Node, recent browsers) and keeps the btoa loop for the others. An 8 MiB
readMany answer no longer blocks the server for about 100 ms. The base64
text is unchanged.

Refs #270
RpcSession.receive took any sub or unsub id the client sent. It now
ignores ids that are not safe integers, as it ignores other broken
messages. The client's ids are its integer counter, so nothing changes
for it.
The codec uses Uint8Array.fromBase64 when the runtime has it and keeps
the atob loop for the others. It accepts and refuses the same texts.
The replacer looked at the value after toJSON ran, so a Buffer went out as
{ type: 'Buffer', data: [...] } instead of a $bin tag. It now checks the
raw value first, like the Date branch.
@Exeloo
Exeloo requested a review from Tchips46 as a code owner November 29, 2026 11:19
@github-actions github-actions Bot added the packages:rpc Related to RPC package label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:rpc Related to RPC package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Base64-encoding readMany answers blocks the server for about 100 ms per 8 MiB RPC stream events are serialized and parsed three times each

1 participant