Skip to content

Home: a key press no longer costs a late frame - key tables update in place, the catalog sync touches only changed rows, the hold band has hysteresis - #858

Merged
GLinnik21 merged 14 commits into
mainfrom
crx-home-hold
Oct 20, 2026
Merged

GLinnik21 merged 14 commits into
mainfrom
crx-home-hold

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

On the TV, every Down/Up on Home cost one late frame (about 24 ms) while scrolling a long Home. Measured with home-grid-deep: 170 shelves, a key every 0.33 s, panel on, release build. 338 of 340 key presses produced one late frame.

Cause (measured on the TV)

Each Down moved the shared hold band:

  1. HubsCmd::Hold released the row that left the band and re-read the row that entered it.
  2. When that re-read landed, Home's sync_catalog re-projected all 170 rows and 2048 items.
  3. prune_keys then rebuilt every key index (rebuild_indexes) to drop about 10 of about 4000 keys.

One instrumented TV run (every frame logged, sched trace) attributed the late frame's step:

  • dstep 23.7 ms median: rebuild_indexes 17.2 ms (73%), the drop scan 1.6, rows / elem_at / shelves 1.5 / 1.5 / 0.6.
  • Next frame: a state_hash of 4.7 ms.
  • Thread state: the frame thread was 92% on-CPU, 8% preempted by system threads, never blocked. So the work itself was the cost, not a wait.

The code came from #413, #605 and #609.

Fix (shared layers; Home and the Library both use them)

  • Key tables update in place. registry::Compaction drops departed keys in place and the indexes follow. Identities are Arc<str>. The item-key sweep runs once per 256 departed keys; that is a batch size, not a cap. The Library's KeyRegistry::prune uses the same compaction.
  • Home's catalog sync is O(changed rows). A row whose identity and epoch are unchanged keeps its projection. elem_at and the key table are patched for the changed rows only, with a full rebuild if a publication ever carries two rows with one identity. A remount, adopt_shows or restore() marks every row stale.
  • Hold-band hysteresis. ui::cards::HoldBand (margin 2, slack 2) re-sends the band only when the rows in view near its edge. It judges coverage against the band the store actually keeps, which is plx_base::hold::clamp_hold (HOLD_ROWS), not the band it asked for. A const assertion ties rows in view + 2×(margin+slack) to HOLD_ROWS. Every row stays reachable.
  • state_hash is computed only for a tap that reads it. That tap is the replay recorder. Tap::state(f, hash_thunk) makes it impossible to implement one without the other.
  • Store. The held merge runs once plus an in-place patch, instead of twice over every source per press.

Verification

  • Red first:

    • a_ring_step_costs_the_rows_it_touches_not_the_catalog: 170 vs 10 hubs, equal index work, no rebuild_indexes.
    • the_hold_band_speaks_once_per_slack_and_never_leaves_the_view_short_of_its_margin.
    • The review's two blockers:
      • Home restored after an eviction landed focus on the wrong card: left 4096 vs right 4098.
      • BACK to the top of a deep Recommended page left rows 0..4 blank with no read in flight.
    • A property test, incremental Home sync vs full re-projection over 150 seeds × 24 steps. It is red with the invalidation removed.
    • merge_held_equals_a_merge_of_the_settled_ring, with two sources, a deck, a pin filter and an unreleasable row.
  • Gates on the committed tree (git status --short empty before and after):

    • make check exit 0: 29 test result: lines, 18325 passed, 0 failed; 103 steps — all ok; harness ok.
    • make test-hostsim ok. 931 passed; 0 failed.
    • make check-nodef clean.
    • make check-replay 3 fixtures, 6 replays, 0 failures.
  • TV (release build, 60 Hz, panel on, muted, mock only), 0f2687c:

    scene result
    home-grid-deep 60 fps, loop robust_min 58 / median 60 (was 56 / 52). Late frames ≥25 ms went from 382 to 32, none from a key press (was 336). The other 23 are clear-call stalls, under investigation separately.
    library-recommended 60 fps, 0 late frames
    library-scroll 60 fps, 0 late frames
  • Host (sim, 170 shelves): the landing-frame key step went from 3.0–3.5 ms to 0.7–0.8 ms.

  • One review: two blockers and four shoulds, all fixed here with tests. It also checked these and found them clean:

    • the state_hash gate: nothing in production reads it, and idle detection does not use it;
    • compaction never reuses an elem;
    • KEY_PRUNE_BATCH is not a cap on reachable content;
    • the layers.
  • The new plx_base::hold module is registered in ci/module-layers.ini. No crate, Cargo, Makefile, build.rs or workflow change, so the crate graph is unchanged and there is no build-bench table.

Not in this PR

The Library's Recommended page with 170 shelves still has a per-key cost: library-shelves-deep showed 166 key frames with a 15 ms median step. That scene's walk also failed to enter the grid on this run. Both are being diagnosed separately. This PR does not merge until it is shown whether that failure predates the change.

🤖 Generated with Claude Code

GLinnik21 and others added 14 commits October 20, 2026 04:43
…d band's hysteresis (red on main)

A one-row ring step over 170 hubs rebuilds both key indexes and rewrites every element of the
catalog (2270 index insertions, 2040 element-map writes against 24 for 10 hubs), and every
one-row scroll sends the store a new band. Both tests fail on main; the fix follows.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… (Home and the Library)

HoldBand re-sends only when the band last sent no longer covers the rows in view and their
two-row margin, and then asks for two more rows each side, so a walk down a long page sends one
band per two rows instead of one per press. Every row stays reachable: the band follows the view.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…e rows that changed

A prune used to rebuild both key indexes from the table (every identity cloned and hashed to drop
about ten keys); it now removes the entries from the table and moves the positions of the rest
(registry::Compaction, shared by Home and the Library's KeyRegistry), and the identity strings are
Arc<str>. sync_catalog keeps the projection, the shelf and the element-map entries of a row whose
identity, title and cards (Arc::ptr_eq) or descriptor size are unchanged, so a landing that changes
two of 170 rows projects two rows. The ring test now drives the store's own Hold and reload paths.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… reads it

Dispatcher::state_hash walks every live instance's LogicalState, which for Home is the whole key
table, so the unconditional dhash on each event frame cost 4.7-5.3 ms on the frame after every
Home store landing (DEBUG-7b). Its only consumers are taps that record or grade it, so Tap gains
wants_state (default false; the fixture RecTap says true) and the frame computes the hash, and
sets FrameReport::state_hash, only when the tap asks. The recorder and replay hash through
Dispatcher::state_hash directly and are unaffected; the debug build's section 5.4 assertion still
hashes. Test: an unread hash is not computed; the prepare-does-not-change-the-hash test now uses
a tap that wants it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…tead of building it twice

A row that gives its cards up publishes as many placeholders as it showed, so releasing it changes
nothing about the catalog's shape. merge_held used to build the catalog, settle the ring, and build
the whole catalog again whenever a row was released (every Down key that moves the band); it now
overwrites the released rows' slices of the first build. settle_ring reports which rows it released,
and the hero pool is lifted out and placed back (place_heroes), looking for a card at the slot it
held before it scans the catalog. A test pins the patched catalog to the second merge it replaces,
hero pool included.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…per tick

merge_with_scope reserves the catalog, its hub rows and the per-card handle table before filling
them, published_order reserves its order, and pump_reloads asks wanted_reloads for the deck offset
instead of computing the published order a second time for it. catalog_bytes sizes a run of one
shared placeholder once (the figure is the same), so the landing's log line no longer reads every
placeholder of a released row. A host sample of the Home walk showed the last of these as the
store's own share of a landing after the earlier changes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…paction has its own test

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Closing the gaps of the dropped placeholder keys moves every key behind them in the table and in
the indexes that name positions, so a publication that changed a row or two still paid for the
whole table (49 of 71 sampled ms of Home's catalog sync on the host walk, nearly all of it hash
probes over the placeholder slots). prune_keys now counts the keys that leave the page and sweeps
the item keys when KEY_PRUNE_BATCH have, or at once when the table is over its bound; the groups
(few) still go at once. A volatile key's identity carries its publication, so the keys that wait
can never be named again. Tests: a ring walk sweeps once per batch, not per publication; the two
tests that asked for a drop within one sync now ask for the batch bound.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…cards without collecting them

A merge of 170 rows allocated a placeholder card for every released row and a vector of cards for
every shelf. It now allocates one placeholder per source (every released row of the source
publishes it) and filters a shelf's cards as it appends them. Rows publish the same cards.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… rows list the elements the key table now holds

adopt_shows swaps element numbers between keys and restore rewrites the places they remember, both
relying on the next sync to project the rows again. Since the sync keeps a row whole while its
cards are the same Arcs, a remount's Continue Watching row hit that fast path and kept the elements
the fresh page minted: the restored focus landed on an unrelated card whenever the next episode came
back in another column, and the key table disagreed with the tile at that element from then on.

Every row now carries a stale mark that invalidate_projection sets (from restore and from an
adopt_shows that changed anything) and the fast paths honour. The wasted rebuild_indexes in
adopt_shows is gone: swapping elements changes neither identities nor positions. The new test
compares the rows, each key's remembered place and the element map with what a full projection
over the same key table gives.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…nd it asked for

The store keeps at most HOLD_ROWS rows of what a screen asks, cut around the middle. The band
recorded what it ASKED, so Recommended's BACK to the top from rows 20..23 (the focused row 0 folded
into the rows in view: an ask of rows 0..27, kept as 5..20) called rows 0..4 covered and sent
nothing more as the view glided up: the top of the page stayed placeholders with no read in flight.

HOLD_ROWS and clamp_hold move to a base module, hold, that the store re-exports and HoldBand
applies, the way Tile lives in base because data and ui cannot name each other: one cut for every
caller. The band remembers the cut of what it sent, asks again as the glide shortens the span, and
does not resend a band the store would keep exactly as it keeps the one it has (a span too tall for
any band). The rule the band relies on, the rows in view plus 2 * (margin + slack) fit the store's
rows, is a compile-time assertion beside the constants. Tests: the shared band against a store that
applies the cut, and the Library page's BACK through the real store.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…random histories, and a hub published twice keeps its second row's elements

The sync's fast path and its patched element map are only right if they equal a projection from
scratch; the new property test drives random publications, evict-and-remount, in-place restore and
promote sequences and compares every step. The store does not rule out two hubs with one identity in
a publication, so the element-map patch now falls back to a full rebuild when two rows share a
group. The merge_held test also covers a second source, Continue Watching, a pin filter and a row
that cannot release its cards.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…band-count bounds match the real cadence

A tap reads the logical-state hash by implementing one method that is offered the hash as a thunk,
so a tap can no longer implement state() and forget the separate wants_state() opt-in (the
recorder would silently have written nothing). The HoldBand doc states the failed-row retry
cadence (a band is sent once per HOLD_SLACK + 1 rows, so a stalled row is retried about once per
three rows, not once per row), the Home comment names the margin and slack the band really asks
for, KEY_PRUNE_BATCH's doc no longer claims a bound it does not keep, and the two band-count tests
bound one band per three rows.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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