repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

perf(firefox-bidi): single big serialization per oversized frame

8621a9dc0719e73bcb9ee28fa645821e72556505

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

CPU profile + controlled experiment (big=3 vs big=0 fixture) showed ~94%
of batch hot-path cost in the oversized-result path, which paid two 512KB
serializations: the full-frame size probe and the spool payload.

Serialize the result first; it decides the spool path and is written
verbatim when oversized. The pre-marker marker.size semantics are
preserved exactly by wrapping a 0 placeholder result once and
substituting its span; borderline threshold gains 1KB wrapper slack.

Paired Wilcoxon vs frozen baseline (output gate bit-identical):
- micro n=30: 3.11 -> 1.89 ms median, p=1.7e-6
- e2e n=12 x3: ~57 -> ~28 ms median, p<=0.015 each

Methodology and measurements: __bench__/README.md

Changed files (3)

extensions/firefox-bidi/__bench__/README.md

Modified
42 42 optimizations kept: single serialization per response (size check shares the
43 43 payload text), `interpolate` fast-path for strings without `{{`.
45 +Follow-up (profiled, 2026-09-26): V8 `--cpu-prof` over the micro loop showed
46 +~49% of samples in the two `runBatch` implementations' own bodies and only
47 +~5% in callees; a controlled experiment (`big=3` vs `big=0` fixture) pinned
48 +~94% of candidate hot-path cost on the oversized-result path, where each big
49 +frame paid **two** 512 KB serializations: the full-frame size probe plus the
50 +spool payload.
52 +Fix: serialize the result first. `resultText` decides the spool path and is
53 +written verbatim when oversized — one big serialization per frame. The old
54 +marker semantics (`size` = pre-marker full-frame length) are preserved
55 +exactly by wrapping a `0` placeholder result once (small stringify) and
56 +substituting its span: `fullLength = probe.length - 1 + resultText.length`.
57 +Borderline threshold uses `MAX_INLINE - 1024` wrapper slack so marked frames
58 +stay under the inline cap.
60 +| run | two-pass median | single-pass median | paired diff | p |
61 +|---|---|---|---|---|
62 +| micro n=30 | 3.11 ms | 1.89 ms | ≈1.2 ms | 1.7e-6 |
63 +| e2e n=12 | 58.4 ms | 27.7 ms | ≈30 ms | 0.0047 |
64 +| e2e replicate | 56.2 ms | 30.2 ms | ≈26 ms | 0.015 |
65 +| e2e replicate | 56.8 ms | 27.9 ms | ≈29 ms | 0.0096 |
67 +≈ **50% faster batches** with oversized payloads, replicated; output gate
68 +(bit-identical payloads vs frozen baseline, including `marker.size`) green.
69 +Small results serialize twice (probe + wrapper) — measured free (the
70 +`big=0` run medians 0.14 ms for 25 frames).
45 72 ## Decision
47 74 Deferred spool drain is behavior-preserving (files are flushed before the

extensions/firefox-bidi/__tests__/lib.test.ts

Modified
1 1 import assert from "node:assert/strict";
2 -import { mkdtemp, readdir } from "node:fs/promises";
2 +import { mkdtemp, readdir, readFile } from "node:fs/promises";
3 3 import { tmpdir } from "node:os";
4 4 import { join } from "node:path";
5 5 import { test } from "vitest";
241 241 new Map(),
244 - const spooled = responses[0].result as { spooled: string };
244 + const spooled = responses[0].result as { spooled: string; size: number };
245 245 assert.ok(spooled.spooled.startsWith(dir));
246 246 assert.equal((await readdir(dir)).length, 1);
247 + const file = await readFile(spooled.spooled, "utf8");
248 + assert.equal(JSON.parse(file).data.length, 300 * 1024);
249 + assert.equal(
250 + spooled.size,
251 + JSON.stringify({
252 + id: 1,
253 + type: "success",
254 + result: { data: "A".repeat(300 * 1024) },
255 + method: "browsingContext.captureScreenshot",
256 + }).length,
257 + "marker.size equals the unspooled full-frame length",
258 + );
249 261 test("runBatch flushes deferred spool writes before returning", async () => {
343 355 assert.deepEqual(ledger.preloadScripts, []);
347 358 test("trimById passes small maps through unchanged", () => {
348 359 const byId = new Map([
349 360 [1, res(1, { a: 1 })],
360 371 assert.equal(trimmed.has(100), false);
361 372 assert.equal(trimmed.get(101)?.id, 101);
362 373 assert.equal(trimmed.get(600)?.id, 600);
363 -});
374 +});

extensions/firefox-bidi/lib.ts

Modified
145 145 byId.set(frame.id, res);
146 146 const out: BidiResponse = { ...res, id: frame.id, method: frame.method };
147 - let text = JSON.stringify(out);
148 - if (text.length > MAX_INLINE && res.type === "success") {
149 - const file = join(
150 - b.spoolDir,
151 - `${frame.method.replace(/[^\w.-]/g, "_")}-${frame.id}.json`,
152 - );
153 - const data = JSON.stringify(out.result ?? null);
154 - out.result = {
155 - spooled: file,
156 - size: text.length,
157 - note: "full result written to file; read selectively",
158 - } as unknown;
147 + let text: string;
148 + // Serialize the result first: it sizes the spool decision and, when
149 + // oversized, is the only big serialization this frame pays (the old
150 + // size-probe on the full frame plus a second result pass cost 2x).
151 + // Small results serialize twice (probe + wrapper); measured free.
152 + if (res.type === "success" && out.result !== undefined) {
153 + const resultText = JSON.stringify(out.result);
154 + // wrapper slack keeps borderline frames under MAX_INLINE once marked
155 + if (resultText.length > MAX_INLINE - 1024) {
156 + const file = join(
157 + b.spoolDir,
158 + `${frame.method.replace(/[^\w.-]/g, "_")}-${frame.id}.json`,
159 + );
160 + // exact pre-marker full-frame length without a second big
161 + // serialization: wrap a placeholder result, substitute its span
162 + const probe = JSON.stringify({ ...out, result: 0 });
163 + const fullLength = probe.length - 1 + resultText.length;
164 + out.result = {
165 + spooled: file,
166 + size: fullLength,
167 + note: "full result written to file; read selectively",
168 + } as unknown;
169 + text = JSON.stringify(out);
170 + // Write off the critical path: frames keep flowing while the file
171 + // flushes; the drain below completes before we return.
172 + spoolWrites.push(writeFile(file, resultText));
173 + } else {
174 + text = JSON.stringify(out);
176 + } else {
159 177 text = JSON.stringify(out);
160 - // Write off the critical path: frames keep flowing while the file
161 - // flushes; the drain below completes before we return.
162 - spoolWrites.push(writeFile(file, data));
164 179 responses.push(out);
165 180 texts.push(text);
266 280 // Cross-call {{N.result.path}} seeds grow without bound over a session;
267 281 // keep only the most recent `keep` frame ids (id order == send order).
268 282 export function trimById(