Skip to content

perf: attribute what survives a clear, instead of naming the gap - #252

Merged
Huxpro merged 4 commits into
new-lynxfrom
perf/lynx-heap-retention-attribution
Aug 31, 2026
Merged

Huxpro merged 4 commits into
new-lynxfrom
perf/lynx-heap-retention-attribution

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 31, 2026 •

Copy link
Copy Markdown
Owner

Summary

#241's retention oracle is a scalar: one post-collection Runtime.getHeapUsage reading. A scalar can say a 3.3 MB gap exists (heapMtsAfterClear 10.21 vs 7.37 MB) and can never say who owns it — #230's rule that a named remainder nominates nobody applies to a heap number exactly as it applies to off_boundary. That is why E2 closed on clear cost and explicitly did not close on retention.

This adds the instrument that turns the gap into named buckets, and the recipe that lets someone else re-run it.

  • benchmarks/lynx-table/stages/heap-retention-analyze.mjs — the pure half. Folds a V8 heap snapshot into bucket -> {bytes, count}, diffs two aggregates, and ranks growth. Separate from the probe because it decides a claim, and a claim generator is worth testing without spending a measurement window.
  • benchmarks/lynx-table/stages/heap-retention-analyze.test.mjs — 10 tests, red-verified.
  • benchmarks/lynx-table/stages/heap-retention.mjs — the driver. Four captures on a fresh page per repetition: fresh, afterCreate, afterClear, and a second create-and-clear as afterClear2.
  • docs/measurement/heap-retention-attribution.md — the recipe, beside heap-after-clear.md.

The fourth capture is what makes the other three readable. One cycle cannot distinguish an array still holding rows from an array that grew a backing store for them and kept it. Cycle two draws that line, and it is the difference between a leak and a high-water mark.

Two records, because the base moved under this branch

This branch was cut at 432c64859. Merging the live base brought in #250, which folds a compile-time-known text child onto its text host — so a row now paints six elements where it painted seven. This probe's largest managed-heap row is one WeakRef per painted element, so that is not a detail: it moves the headline number.

Both records are checked in. Same probe, same scale, same core, different code:

c230-retention-10000 c252-postcausea-10000
base 432c64859 a4d22e295 (on 4081baf73)
repetitions 1 3
retained over fresh 2.38 MiB 1.17 MiB
object:WeakRef, cycle 1 1,120,000 B / 70,000 nodes 960,000 B / 60,000 nodes
array:, cycle 1 1,047,864 B 519,032 B
array:, cycle 2 1,044,232 B 524,432 B
native:system / JSArrayBufferData, cycle 1 16,056,320 B 14,745,600 B
live array: nodes (afterCreate − fresh) 120,013 110,013

The instrument tracked a known cause to the integer, which is the strongest check it can pass. object:WeakRef falls by exactly 10,000 nodes at exactly 10,000 rows — one per row, matching one folded element per row — and the live array: node count falls by exactly the same 10,000. Cause A's element diet is visible in the retained heap at full precision, and #252's WeakRef mechanism claim comes out of the base change strengthened rather than stale.

The WeakRef mechanism is verified, not hypothesised. Every __Create* in @lynx-js/web-core's main-thread element layer hands a new WeakRef(dom) to the Wasm element table:

__CreateView(parentComponentUniqueId) {
  const dom = document.createElement('x-view');
  dom[uniqueIdSymbol] = wasmContext.create_element_common(parentComponentUniqueId, dom, new WeakRef(dom));

60,000 wrappers at 10,000 rows is exactly 6 per row on the current base (70,000 was exactly 7 before the fold). The retained table shows no x-view, x-text, ShadowRoot or NamedNodeMap row, so the referents died and only the wrappers survived. Cycle two adds none, so the table reuses its slots: bounded, not compounding.

The two c252-postcausea runs — one before the commit stamp existed, one after — reproduce every bucket byte for byte, which is what a structural count should do and a wall-clock number could not.

The record now names the code it measured

The comparison above was only possible because I generated one of the two records myself. Neither file said which commit it came from: the JSON carried Node, Chromium, load and bundle path, and the markdown — the artifact that actually gets quoted into issue comments — carried none of it.

So the generated report now names the commit and whether the tree was dirty, in its own paragraph near the top. dirty: true disqualifies a record for comparison and the report says so in bold. Stamping is never fatal: a checkout without git records a null stamp rather than losing the measurement. c230-retention-10000 predates the stamp; its base is recorded in the recipe doc instead.

Validation

  • targeted tests: node --test benchmarks/lynx-table/stages/heap-retention-analyze.test.mjs — 10 pass, 0 fail on the merged base, red-verified with two deliberate probes (hardcoding the self_size offset broke the offsets test; dropping shrinking buckets broke the negative-rows test; both restored).
  • the driver ran end-to-end at 10,000 rows on the merged base, 3 repetitions, quiet host (load 0.04/CPU), and produced the checked-in artifact.
  • pnpm sync clean; pnpm format:files on both changed files.
  • pnpm format:check / pnpm typecheck / pnpm test — not run repo-wide. This branch touches only benchmarks/lynx-table/stages/ and docs/, no published package source.

The 1,000-row record was produced before the second-cycle capture existed and its shape no longer matches what the writer emits — the same staleness that makes stages/results/issue57-clear-teardown.json fail today, so it was dropped rather than committed. Its array: figure is still quoted below because it is a bucket total, not a record shape.

Risk / follow-ups

  • array: is still unowned, and is now the dominant named managed-heap grower — 90.4% of the second cycle's retention against 42.2% of the first. It halved with Cause A (1,047,864 → 519,032 B) but still grows by roughly its own size every cycle, so it is unbounded exactly as before.

    The follow-up Order now has a prediction to confirm rather than a graph to fish in. The bucket's net growth matches, at every measured point, a single 8-byte-per-slot array whose capacity is the smallest power of two ≥ the live painted-element count:

    point elements next 2ⁿ × 8 B observed delta
    1k rows, pre-Cause-A 7,000 8,192 65,536 64,824 −712
    10k rows, pre-Cause-A 70,000 131,072 1,048,576 1,047,864 −712
    10k rows, post-Cause-A, cycle 2 60,000 65,536 524,288 524,432 +144

    The residual is the same constant at two scales three doublings apart, which is what a fixed offset from the rest of the bucket looks like and what a curve fit does not. Since object:WeakRef is exactly one per painted element and lives in web-core's Wasm element table, the natural candidate is that same table's backing store — which would make the two largest managed-heap rows one owner, not two.

    Stated as a hypothesis under The create residual: attribute it, tax-test it, then extend compiled programs to steady-state mounts (E-train) #230: it is arithmetic agreement across three (scale, code) points, not a retainer path. Confirming it needs the edge-graph walk this probe deliberately does not do, and naming the owner without that walk would be the exact move this instrument exists to prevent.

  • This changes what the oracle means, not what the runtime does. No package source is touched.

  • Branch naming. The harness assigned claude/octane-issue-66-phase-a-7wz97z for this session, but create-a-pr requires a feat/|fix/|docs/|test/|perf/ topic branch so label-pr.yml can read the type. A topic branch was used; flagging the conflict rather than resolving it unilaterally.

  • Self size, not retained size: a bucket names an owner-of-bytes, never a root cause.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

Huxpro and others added 3 commits August 31, 2026 07:11
A record that does not name its code cannot be compared to another one, and
this probe's largest managed-heap row is one `WeakRef` per painted element —
so its readings move whenever the compiler changes how many elements a row
paints.

That is not hypothetical. The record checked in with this probe was taken
before #250 folded a compile-time-known text child onto its `text` host. Taken
again after, on the same probe at the same scale, the retained total is half
what it was and the `WeakRef` row falls from 70,000 nodes to 60,000 — exactly
one fewer per row. A reader holding both files could not have told which was
which, because neither said.

The generated report now names the commit and whether the tree was dirty, in
its own second paragraph rather than only in the JSON, because the report is
the artifact that gets quoted. Stamping is never fatal: a checkout without git
records a null stamp rather than losing the measurement.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
…lements

Same probe, same scale, same core, on the current base rather than the one this
branch was cut from. Every bucket reproduces the unstamped run that preceded it
byte for byte, which is what a structural count should do.

What moved, and it is not noise:

- `object:WeakRef` falls 70,000 -> 60,000 nodes. That is exactly one fewer per
  row at 10,000 rows, and one `WeakRef` per painted element is this probe's one
  verified mechanism. The instrument tracked a known element-count change to
  the integer.
- The live `array:` row falls 120,013 -> 110,013 nodes, the same exact 10,000.
- Retained over `fresh` halves, 2.38 -> 1.17 MiB.

The earlier record is kept rather than replaced. It measures different code and
neither supersedes the other; the recipe now names the commit each was taken at
so the pair can be read as the comparison it is.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro
Huxpro marked this pull request as ready for review August 31, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant