pi-ext bugabingas pi extensions
owned by admin
README Source History Refs Compare Notes Search perf(firefox-bidi): single big serialization per oversized frame 8621a9dc0719e73bcb9ee28fa645821e72556505
Oliver Jan Krylow <oliver@bugabinga.net > · 26 Sep 2026
SSH signature · not checked
Browse files Compare 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
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).
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" ;
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 );
254 + result: { data:
"A" .
repeat (
300 * 1024 ) },
255 + method:
"browsingContext.captureScreenshot" ,
257 + "marker.size equals the unspooled full-frame length" ,
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 );
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" ) {
151 - `${frame. method . replace (/[ ^ \w.-]/ g , "_")}-${frame. id }.json` ,
153 - const data
= JSON .
stringify (out.result
?? null );
157 - note:
"full result written to file; read selectively" ,
152 + if (res.
type === "success" && out.result
!== undefined ) {
153 + const resultText
= JSON .
stringify (out.result);
155 + if (resultText.
length > MAX_INLINE
- 1024 ) {
158 + `${frame. method . replace (/[ ^ \w.-]/ g , "_")}-${frame. id }.json` ,
162 + const probe
= JSON .
stringify ({
... out, result:
0 });
163 + const fullLength
= probe.
length - 1 + resultText.
length ;
167 + note:
"full result written to file; read selectively" ,
169 + text
= JSON .
stringify (out);
172 + spoolWrites.
push (
writeFile (file, resultText));
174 + text
= JSON .
stringify (out);
159 177 text
= JSON .
stringify (out);
162 - spoolWrites.
push (
writeFile (file, data));
164 179 responses.
push (out);
165 180 texts.
push (text);
268 282 export function trimById (