Skip to content

perf(snapshot): build direct rendered text in one loop, not three arrays per element - #48

Merged
capy-ai[bot] merged 1 commit into
mainfrom
capy/build-direct-rendered-text
Dec 3, 2026
Merged

capy-ai[bot] merged 1 commit into
mainfrom
capy/build-direct-rendered-text

Conversation

@jaredLunde

Copy link
Copy Markdown
Contributor

directRenderedText in agent/src/snapshot.js allocated three arrays per element (Array.from(childNodes), a filter, a map, another filter) to build one string. On the 21,916-element fixture it runs for 11,668 elements and was the second-largest leaf of the snapshot walk. It is now a single index loop over childNodes.

Why

Measured while attributing the action path (CAPY-28). The walk is the constraint: a trivial browser_evaluate on the heavy fixture is 95 ms, of which 92.7 ms is one page-side snapshot evaluation; the Rust side of the action is 1.4–2.4 ms.

  • In-page A/B of the shipped script against this variant (5 rounds, identical outline hash): heavy 112.2 → 98.1 ms, mid 35.1 → 33.2 ms, small unchanged.
  • Real path, warm snapshot helper, 40 interleaved rounds on the heavy fixture: 116.6 → 102.6 ms median snapshot, paired difference median 8.2 ms (mean 8.8). Mid (30 rounds) and small (30 rounds) are within noise.
  • 12 interleaved rounds showed nothing; the medians only separate at 40 rounds on this box.

Proof

  • 544 answers byte-identical to 2be3331: 408 across four shaping budgets (default, 65536×200, 32768×120, 16384×80) over tiny/small/heavy/forms; 40 live-ref answers over five pages; 96 find/snapshot answers over six pages including an 8k-element mid fixture. The find needles matter because a rendered name is only observable through browser_find.
  • New gate in mcp/tests/fixtures/snapshot_w2_test.js: fails if the extractor stops being a braced loop, if Array.from returns, or if .filter(/.map( returns to its body; plus a behavioural case pinning the join ("Alpha beta Gamma" from five text nodes, two blank).
  • Mutants: restore the array pipeline → gate red; keep the loop and re-add Array.from(childNodes) → gate red while the behavioural case stays green (the work gate, not the text gate, is what catches it); change the join separator → behavioural case red.
  • Suites at the head: core 414; CLI 11 + 1 (3 ignored, pre-existing); MCP 65 + 1 + 33 + 1; agent 117 + 6 browser gates (--test-threads=1).
  • Clippy vs 2be3331: identical output for core, CLI, MCP and agent after path normalisation.
  • No flag, no payload change, no docs change: the delivered bytes, the ref numbering and the wire shape do not move.

Open in Capy

The extractor allocated three arrays per element (childNodes copy, filter,
map) to build one string, for 11,668 elements on the 21.9k-element fixture.
One index loop produces the same text; heavy snapshot 116.6 -> 102.6 ms in
40 interleaved real-path rounds, mid/small within noise. Adds a source gate
and a behavioural case; 544 answers byte-identical to the base.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai

capy-ai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Measurement context for this change (CAPY-28):

  • The snapshot walk is the action path's constraint: a trivial browser_evaluate on a 21,916-element page is 95 ms, of which 92.7 ms is one page-side snapshot evaluation.
  • Inside the walk, the direct-text extractor was the second-largest leaf at 20 ms; it allocated three arrays per element for 11,668 elements.
  • Real path, 40 interleaved rounds: heavy snapshot 116.6 → 102.6 ms median; mid/small within noise. 12 rounds show nothing on this box — the paired medians only separate at 40.
  • 544 answers byte-identical (four shaping budgets, live refs, and find/snapshot over six pages including an 8k-element mid fixture).

@capy-ai
capy-ai Bot merged commit 9d54fa4 into main Dec 3, 2026
3 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.

1 participant