perf(sessions): incremental list scan — fingerprint memoization eliminates 70s cold load - #625
Draft
Nuctori wants to merge 1 commit into
Draft
perf(sessions): incremental list scan — fingerprint memoization eliminates 70s cold load#625Nuctori wants to merge 1 commit into
Nuctori wants to merge 1 commit into
Conversation
…nates 70s cold load SDK SessionManager.listAll replays every byte of every session (~9.5 GiB) to build allMessagesText, which pi-web never consumes. Replace the inner scan with a pure-function memoization: F(file) -> ScannedSessionInfo (1:1 mirror of SDK semantics for the fields pi-web actually reads: id/cwd/name/created/modified/ messageCount/firstMessage/parentSessionPath) Invariant (append-only): pi only appends lines or rewrites wholesale; either changes (size, mtimeMs). So fingerprint fp=(size,mtimeMs) unchanged => bytes unchanged => cached F(file) === fresh scan. Changed/deleted/new files are always rescanned/dropped/added, so the incremental set equals a full rescan and sort(modified desc) matches SDK. Effect: cold /api/sessions 70s at 1992 sessions -> warm-hit stat-only pass (~200ms) with at most changed files rescanned; persisted index (pi-web-session-index.json, atomic 0600) survives restarts and is revalidated by stat on every load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the inner scan of
SessionManager.listAllwith a pure-functionfingerprint memoization in
lib/session-list-scanner.ts, then routesloadAllSessions()through the newlistSessionsIncremental()entry.F(file) -> ScannedSessionInfomirrors the SDK'sbuildSessionInfoline semantics 1:1 for the fields pi-web actually reads
(
id/cwd/name/created/modified/messageCount/firstMessage/parentSessionPath). The dropped field isallMessagesText, whichpi-web never consumes (grep
lib//app//components//hooks/returns zero non-test references).
~/.pi/agent/pi-web-session-index.json(versioned, mode 0600, atomic write via
writePrivateFileAtomicSync).Restarts re-validate entries by
stat; nothing else changes.MAX_CONCURRENT_SCANS = 10workers over the changedset; unchanged files contribute zero IO.
Why
/api/sessionscold scan replays every byte of every JSONL (~9.5 GiBacross 1992 sessions) just to assemble a single string nobody reads. The
diag on the local catalog (cold 70085ms, warm 214ms, perf-bench 908ms
warm) shows the regression surface: the route is the front door, so
this is P0.
Invariant (formal)
SessionManager.listAllis replaced bylistSessionsIncremental. Foran append-only JSONL:
Changed / deleted / new files are always rescanned / dropped / added, so
the incremental set is the full set. Sort by
modified descmatchesSessionManager.listAll. ThereforelistSessionsIncremental() === listAll()on
(sessions, sort)for the fields pi-web reads.No source changes outside the two files
app//components//hooks//e2e/package.json/package-lock.jsonlib/session-list-scanner.ts(new),lib/session-reader.ts(3-line wire:
loadAllSessionscallslistSessionsIncrementalinsteadof
SessionManager.listAll)Audit (self)
origin/main(28bab3c), ahead 0 before this PRnode_modules/.bin/tsc --noEmit)lib/session-list-scanner.ts; revert the 3-linewire in
lib/session-reader.ts; no other touchesunchanged => cached F unchanged); incremental set equals full set by
construction
Stacked plan (each PR base=main, merge-order-free)
perf/bench— independent perf-bench job, no e2e harness depperf/l1-scanner— fingerprint memoizationperf/l2-tail—O(delta)tail-read for grown files (~140 lines)perf/l3-sidebar— windowed list (~85 lines)perf/l4-context— reverse-streamloadRecentEntries+ test fix(~135 lines)
P2 stands alone: even without P1's CI gate, the local diff stats
(cold 70s → warm 200ms on the local 1992-session catalog) justify
landing it.