Skip to content

perf(tui): session browse cache for instant Nav kind switches - #88

Merged
lance0 merged 1 commit into
masterfrom
perf/tui-browse-cache
Jun 25, 2026
Merged

lance0 merged 1 commit into
masterfrom
perf/tui-browse-cache

Conversation

@lance0

@lance0 lance0 commented Jun 25, 2026

Copy link
Copy Markdown
Owner

What

Switching Nav kinds re-fetched the whole browse every time, so returning to a kind you'd already loaded blanked the list until the round trip landed. This caches browse results for the TUI session, keyed by (kind, active filter), and repaints from cache immediately while a normal background browse refreshes the list (stale-while-revalidate).

Behavior

  • Instant revisit — returning to a previously-loaded Nav kind repaints the cached list at once; a background Browse still runs and adopts the fresh result when it lands.
  • Selection restored by identity — stored as (kind, id), not a row index, so a reordered refresh keeps the right row highlighted; a row that's gone falls back to the top.
  • Filtered vs unfiltered are separate keys — clearing a / filter snaps back to the full list instantly.
  • Profile switch drops the cache — no cross-instance data bleed.

Correctness (the race story)

  • Every cache-backed switch still returns a Browse command, and stamp_request_ids bumps browse_gen for it — so the background refresh supersedes any in-flight browse of the prior kind, and is_current_browse(req) drops the loser.
  • The BrowseComplete stale-guard runs before adopt_fresh_browse, so a superseded response can't poison the cache.
  • Switching to Recent calls invalidate_browse_requests (bumps browse_gen past any in-flight browse), so a late response can't repopulate the cleared list.
  • BrowseComplete now carries the filter that was actually fetched, so the response lands in the correct cache entry.

Tests

3 new pure-handler tests over the existing scaffold:

  • cached_browse_repaints_immediately_and_refreshes_in_background (instant repaint + background Browse dispatched + prior selection restored),
  • browse_cache_keeps_filtered_and_unfiltered_lists_separate,
  • recent_invalidates_in_flight_browse (late response can't repopulate Recent).

Plus the profile-switch test now asserts the cache is dropped.

Verification

  • fmt clean; clippy --all-targets clean on --all-features and --no-default-features; 1142 all-features / 1047 no-default tests pass, 0 failed.

Notes

  • Session-scoped cache, no TTL/LRU. Bounded in practice — cleared on profile switch, ~20 kinds, each entry ≤ BROWSE_CAP rows — but distinct filter strings accumulate for the session. A soft cap is easy to add later if marathon sessions with many filters become a concern.

Switching Nav kinds re-fetched the whole browse every time, so returning to a
kind you'd already loaded blanked the list until the round trip landed. Cache
browse results for the TUI session, keyed by (kind, active filter), and repaint
from cache immediately while a normal background browse refreshes the list
(stale-while-revalidate).

- Selection is restored by identity ((kind, id), not index), so a reordered
  refresh keeps the right row highlighted; a vanished row falls back to the top.
- Filtered and unfiltered lists are separate cache keys, so clearing a `/` filter
  snaps back to the full list instantly.
- Race-safe: every cache-backed switch still dispatches a Browse, which bumps
  browse_gen via stamp_request_ids, so the background refresh supersedes any
  in-flight browse of the prior kind and the stale guard drops the loser before
  it can touch the cache. Switching to Recent invalidates in-flight browses so a
  late response can't repopulate the cleared list.
- The cache is dropped on profile switch to avoid cross-instance data bleed.

BrowseComplete now carries the filter that was actually fetched so the response
lands in the right cache entry.
@lance0
lance0 merged commit fdcf96d into master Jun 25, 2026
4 checks passed
@lance0
lance0 deleted the perf/tui-browse-cache branch June 26, 2026 20:41
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