Skip to content

perf(lynx): look up a declared host by binary search over ordered runs - #257

Merged
Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-list-run-lookup
Aug 31, 2026
Merged

Huxpro merged 1 commit into
new-lynxfrom
perf/lynx-list-run-lookup

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Summary

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, 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:

 * A linear scan because a run is one command per native list: a tree carries as
 * many of these as it has lists, not as it has rows.

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.

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 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.ts
    • runAtOrBefore: binary search for the one run that can declare an id.
    • declaringRun and runsOverlapRange use it. The overlap check needs only
      the last run starting at or before the range's end: ranges are disjoint and
      sorted, so an earlier run ends before that one begins.
    • insertDeferredRun keeps 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.
    • mergeDeferredRuns joins the accepted and staged lists at accept, replacing
      the concatenation that left the accepted list unordered from then on.
    • The invariant is documented on deferredRuns itself, next to the identical
      one on retiredRanges.
  • packages/lynx/tests/_fixtures/native-list-shape-classes.lynx.tsrx — a keyed
    @for with two branches that are both declarable. The existing
    native-list-mixed fixture cannot show this: one of its branches binds a
    worklet and is refused for a reason of its own.
  • Regressions in list-recycling.test.ts and background-deferred-list.test.ts
    (below).
  • benchmarks/lynx-table/stages/results/issue195-list-run-lookup-2026-08-31.md
    and 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 of count: 8. That
counter 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…of over every run to a binary search, and their
callers — the staging id scan and listItems in the per-commit walk — are named
in 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.

rows window before after
1,000 mount 2.71× 1.98×
2,000 mount 3.84× 1.90×
4,000 mount 7.45× 2.01×
1,000 update 1.50× 1.06×
2,000 update 1.94× 1.15×
4,000 update 3.35× 1.19×

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:

mutation went red
runAtOrBefore returns the last run answers for a row declared by any of many runs, places a run declared out of id order, refuses a run overlapping a declared range that is not the most recent
insertDeferredRun always appends places a run declared out of id order, and only that one
accepted runs concatenated instead of merged places a run declared in a later commit before the ranges already accepted, and only that one

The 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.

    | failing file | assertion | on base |
    | --- | --- | --- |
    | `packages/drei/tests/browser/upstream-e2e.browser.test.ts` | `page.waitForFunction` 30 s timeout (headless WebGL) | fails |
    | `packages/octane/tests/browser/suspense-hydration/suspense-hydration.test.ts` | `[selectionStart, selectionEnd]` after hydration | fails |
    | `packages/rspeedy-plugin-octane/tests/native-demo.test.ts` | `stopProcessTree` escalation at `stopTimeoutMs: 100` | fails |
    
    None is in `packages/lynx`, and this diff is entirely inside it.
    
  • targeted tests: npx vitest run --project lynx — 741 passed, 48 files (736
    on the base; +5 new).

  • pnpm format:files on every changed file; pnpm sync clean.

  • node --test benchmarks/lynx-table/stages/*.test.mjs — 184 pass, 0 fail.

  • pnpm format:check (repo-wide) — not run; the scoped formatter covers the
    changed files.

pnpm typecheck:files on list-recycling.test.ts reports Cannot find name 'Element' and friends. That is the scoped runner mapping a test file onto
packages/lynx/tsconfig.json, which sets lib: ["esnext", "webworker"] and
excludes tests — so no project in the repo typechecks packages/lynx/tests/**
at all, and pnpm typecheck is clean because it never looks there. Verified
rather than assumed: with this diff stashed, the base file reports the same
errors at the same 23 lines. This branch adds three more Element uses of the
same kind and changes nothing about the gap. Noting it as a pre-existing hole
rather than fixing it here.

Risk / follow-ups

  • The insertion's bound. A run declared out of id order costs a linear shift
    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.
  • The run count itself is untouched. One run per row is still one command,
    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.
  • No device claim. The measurement is jsdom on a shared cloud VM. It prices
    the JavaScript, not the platform, and nothing in it is comparable across
    records.
  • Nothing typechecks packages/lynx/tests. Found while checking the errors
    above. tsconfig.json excludes tests, tsconfig.testing.json
    includes only src/testing.ts, and typetests/tsconfig.json covers the type
    tests — so the gate never sees the test sources, and pnpm typecheck:files on
    one of them reports against a project that excludes it. Pre-existing and out of
    scope here; worth its own change.
  • Branch name. The harness for this session assigns
    claude/octane-issue-66-phase-a-7wz97z, which the repo's own create-a-pr
    rules and .github/workflows/label-pr.yml cannot read as a type. The branch
    is perf/lynx-list-run-lookup so the label bot works; flagging the deviation
    rather than hiding it.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

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

Huxpro commented Aug 31, 2026

Copy link
Copy Markdown
Owner Author

CI cannot run on this PR, and it is not this PR

Marking this ready started run 33357745041. One job appeared — classify changeset release — and it has sat queued since 04:37:34Z without starting. Nothing else in the workflow can begin: release_change is ci.yml's root job and every other job carries needs: release_change, so a release_change that never starts wedges the entire run.

This is repo-wide and predates the PR.

The last ci.yml run to produce a pass or a fail was run 434, the new-lynx push for #228, started 2026-08-27T18:56:49Z, concluded success. Every run since — 435 through 503, about 3.4 days and 69 runs across a dozen branches — is one of:

  • skipped, where the draft guard short-circuited before requesting a runner;
  • cancelled, where cancel-in-progress retired a run that was still queued when the next push arrived;
  • queued, still, having never been given a runner.

Five are in that last state right now, and one of them is not a PR at all:

run branch queued since
33357972019 test/lynx-e3-run-slot-update-certification (#253) 04:41
33357745041 perf/lynx-list-run-lookup (this PR) 04:37
33357... (run 498) new-lynx — the base branch's own push run 02:39
run 492 feat/lynx-painted-element-ceiling 00:52
run 490 perf/lynx-heap-retention-attribution 00:31

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 new-lynx either.

What is actually failing

Every ci.yml job requests runs-on: blacksmith-4vcpu-ubuntu-2404, and no such runner is being assigned. The discriminating evidence is label-pr.yml, which declares the same runner label yet completed on this PR at 04:36:09Z: its job reports "labels": ["ubuntu-latest"], runner_group_name: "GitHub Actions" — a GitHub-hosted runner, not Blacksmith. So work reaching a GitHub-hosted runner completes in seconds, while everything waiting on the literal blacksmith-* label waits forever. That points at the Blacksmith runner pool or its dispatch, which is account-side and cannot be fixed from inside a pull request.

What I am not doing

  • Not re-running. Five independently queued runs across four branches, over four hours, already establish it. A re-run re-queues behind the same absent runner and buys no information.
  • Not routing around it. Changing runs-on to ubuntu-latest would start CI, but it rewrites the runner strategy for every job in the repo and changes what CI costs and how long it takes. That is a repository decision, not something to slip into a perf PR.

The lever

Someone 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 new-lynx should drain on their own; if they were cancelled by then, a push or a re-run restarts them.

Meanwhile the local evidence for this diff stands on its own and is written up in the PR body: pnpm typecheck exit 0, pnpm test at 26,785 passing with three failures each verified to fail identically on the base with this diff stashed, and npx vitest run --project lynx at 741 passing.

Same blocker on #253 and #256; noted there rather than repeated.


Generated by Claude Code

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