Repository navigation
perf(lynx): look up a declared host by binary search over ordered runs - #257
Conversation
A native `<list>`'s rows are declared by a `mount-template-run`, and a run covers a contiguous span of one row shape. A keyed `@for` whose body has two branches therefore declares one run per row for a feed that alternates between them, which is the ordinary shape of a real feed. The two lookups that answer for a declared host scanned the run list linearly, on the reading written into the source: "a linear scan because a run is one command per native list". That premise is false, and two readers pay for it once per row -- the staging pass that checks every id a declared range covers, and the per-commit walk that tells a list about its logical rows. Both were O(rows x runs), which is quadratic in the length of a heterogeneous feed. `deferredRuns` is now kept sorted by `firstId` over disjoint ranges, the same shape `retiredRanges` beside it already had, and both lookups binary-search it. Ranges are disjoint because an overlapping declaration is refused where it is staged, so exactly one run can answer for an id. `insertDeferredRun` keeps the staged list ordered and `mergeDeferredRuns` joins the accepted and staged lists at accept, replacing a concatenation that left the accepted list unordered. Regressions cover a row declared by any of many runs, a run declared out of id order, a run declared in a later commit below the accepted ranges, an overlap against a range that is not the most recent, and the deterministic run count that makes the premise false: eight rows of two shapes declare eight runs of count 1 where one shape declares one run of count 8. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
CI cannot run on this PR, and it is not this PRMarking this ready started run 33357745041. One job appeared — This is repo-wide and predates the PR. The last
Five are in that last state right now, and one of them is not a PR at all:
The base branch's own push run has been queued for two hours. Under the usual rule — a check that is red on the base branch too is not this PR's — this qualifies twice over: it is not red, it cannot start, and it cannot start on What is actually failingEvery What I am not doing
The leverSomeone with the Blacksmith dashboard needs to look at it — quota, billing, or the app installation. Once runners are being assigned again, the queued runs here and on Meanwhile the local evidence for this diff stands on its own and is written up in the PR body: Same blocker on #253 and #256; noted there rather than repeated. Generated by Claude Code |
Summary
A native
<list>'s rows are declared by amount-template-run, and a run coversa contiguous span of one row shape. A keyed
@forwhose body has twobranches therefore declares one run per row for a feed that alternates
between them — which is the ordinary shape of a real feed, not a corner case.
The two lookups that answer for a declared host scanned the run list linearly,
on the reading written into the source:
That premise is false, and two readers pay for it once per row: the staging pass
looks up every id a declared range covers, and the per-commit walk that tells a
list about its logical rows reads every row again. Both were therefore
O(rows × runs), and with one run per row that is quadratic in the length of
the feed.
deferredRunsis now kept sorted byfirstIdover disjoint ranges — the sameshape
retiredRangesbeside it already had — and both lookups binary-search it.Ranges are disjoint because an overlapping declaration is refused where it is
staged, so exactly one run can answer for an id and the search can stop at one
candidate.
Why
Found while scoping #193's Layer 1 against the current code. Layer 1 is about
what a row costs to materialize; this is a step below it, in what a commit
costs to carry the declaration at all. Fixing it does not do Layer 1's work, and
it changes what Layer 1 is: with the quadratic gone, one run per row is linear
overhead — an optimization — rather than a cliff a heterogeneous feed falls off.
Changes
packages/lynx/src/core/host-driver.tsrunAtOrBefore: binary search for the one run that can declare an id.declaringRunandrunsOverlapRangeuse it. The overlap check needs onlythe last run starting at or before the range's end: ranges are disjoint and
sorted, so an earlier run ends before that one begins.
insertDeferredRunkeeps the staged list ordered. Ids are minted ascending,so the ordinary case appends; the search exists because the commands come
from a peer and a run declared out of id order still has to be placed
correctly.
mergeDeferredRunsjoins the accepted and staged lists at accept, replacingthe concatenation that left the accepted list unordered from then on.
deferredRunsitself, next to the identicalone on
retiredRanges.packages/lynx/tests/_fixtures/native-list-shape-classes.lynx.tsrx— a keyed@forwith two branches that are both declarable. The existingnative-list-mixedfixture cannot show this: one of its branches binds aworklet and is refused for a reason of its own.
list-recycling.test.tsandbackground-deferred-list.test.ts(below).
benchmarks/lynx-table/stages/results/issue195-list-run-lookup-2026-08-31.mdand the probe it cites,
packages/lynx/tests/_probes/list-run-lookup.probe.ts.The probe is not a test and does not run in CI; the record carries the command
that runs it.
Evidence
Deterministic, in the suite. A two-shape feed of 8 rows declares eight runs
of
count: 1; the same 8 rows in one shape declare one run ofcount: 8. Thatcounter is the premise the old comment denied, and it is what makes the run
count unbounded.
The complexity claim is verified by reading, not inferred from a clock: the
two lookups went from a
for…ofover every run to a binary search, and theircallers — the staging id scan and
listItemsin the per-commit walk — are namedin the source.
Measured, as corroboration. Same fixture, same row count, same rows; the
arms differ only in how many runs those rows are declared as. Ratios, because
the absolutes are worthless on a shared VM; recipe and full samples in the
record.
Before, both ratios roughly double as the row count doubles. After, both are
flat. In absolute terms at 4,000 alternating rows: mount 595.6 → 156.5 ms,
update 307.3 → 115.1 ms, with the homogeneous arm unchanged in both windows —
the ordered lookup costs the one-run case nothing.
Red-verified. Each mutation, and the tests it broke:
runAtOrBeforereturns the last runinsertDeferredRunalways appendsThe first mutation broke three of the four new tests and none of the 36
existing ones in that file, which is the measure of the gap: every other test
there holds one run against one list, and one run cannot tell a search from a
scan.
Validation
pnpm typecheck— exit 0.pnpm test— 2448 of 2451 files pass; 26785 tests pass, 3 fail, 1 skipped.The three failures are pre-existing and unrelated: each was re-run alone
on an idle machine and then re-run alone against this branch's base with the
diff stashed, and each fails identically both times.
targeted tests:
npx vitest run --project lynx— 741 passed, 48 files (736on the base; +5 new).
pnpm format:fileson every changed file;pnpm syncclean.node --test benchmarks/lynx-table/stages/*.test.mjs— 184 pass, 0 fail.pnpm format:check(repo-wide) — not run; the scoped formatter covers thechanged files.
pnpm typecheck:filesonlist-recycling.test.tsreportsCannot find name 'Element'and friends. That is the scoped runner mapping a test file ontopackages/lynx/tsconfig.json, which setslib: ["esnext", "webworker"]andexcludes
tests— so no project in the repo typecheckspackages/lynx/tests/**at all, and
pnpm typecheckis clean because it never looks there. Verifiedrather than assumed: with this diff stashed, the base file reports the same
errors at the same 23 lines. This branch adds three more
Elementuses of thesame kind and changes nothing about the gap. Noting it as a pre-existing hole
rather than fixing it here.
Risk / follow-ups
in the staged array, so a peer that declared a whole feed backwards would pay
for it. Stated in the source rather than designed against: the ordering is
this renderer's own, this runs once per declaration, and the lookups are what
run once per row per commit.
one value table and one declaration per row — the residual ~2× at mount above.
That is Future: compiled main-thread programs inside native list rows — and whether the list API itself is the right surface #193 Layer 1's, and Research: the native list boundary — what the platform can consume today, what a cell re-entry costs, and whether #193's compiled-row surface is buildable #195's device report already gave it a GO.
the JavaScript, not the platform, and nothing in it is comparable across
records.
packages/lynx/tests. Found while checking the errorsabove.
tsconfig.jsonexcludestests,tsconfig.testing.jsonincludes only
src/testing.ts, andtypetests/tsconfig.jsoncovers the typetests — so the gate never sees the test sources, and
pnpm typecheck:filesonone of them reports against a project that excludes it. Pre-existing and out of
scope here; worth its own change.
claude/octane-issue-66-phase-a-7wz97z, which the repo's owncreate-a-prrules and
.github/workflows/label-pr.ymlcannot read as a type. The branchis
perf/lynx-list-run-lookupso the label bot works; flagging the deviationrather than hiding it.
Provenance
agent-authored)Generated by Claude Code