Repository navigation
perf: load only the session-cache members a date range can use - #1542
Merged
Merged
Conversation
A shard is still one JSON object, so older builds read it unchanged, but it is written through a stream and never exists as one string. Past V8's string limit the save used to throw and the cache silently never persisted; reads of such a shard now go member by member.
A ranged load streams its in-scope shards and keeps in full only the files with a call in the range, spawn anchors, failure markers, turn-less files and WSL paths. Every other file becomes a stub: its fingerprint, bucket, PR flag, dedup keys per turn and where its member line lives. A stub stands in for its entry everywhere but the summaries it cannot contribute to: reconcile, the seenMsgIds and parser dedup pre-seeds, the query-time key replay in serve order, orphan surfacing, eviction and the emptiness checks. A stub whose file changed loads its member first, so the append or re-parse runs as before, and a wider request in the same process loads the stubs it now needs. A bucket holding stubs is rewritten as a full load would write it, copying the stubs' lines from the shard they came from; unchanged, the published shard is kept. A single-line shard from an older build is rewritten on the next save. Providers whose parse reads cached entries no report shows (hermes, quickdesk, antigravity, devin, gemini) still load in full.
Discovery only needs each rollout's project, but it loaded the whole codex results file (hundreds of MB on a large corpus) on every run to get it. flushCodexCache now also writes codex-projects.v15.json, keyed by the same fingerprint, and discovery reads that. A missing index is built once from the results file.
status asks for today, then the month. A command can now declare the union of its ranges, so the first load already holds the month in full and the second request finds it there; each request keeps its own month scope and summaries.
Once the refresh lock is acquired or another process completed the refresh, the snapshot loaded before the wait is never used again, but it stayed referenced while the reload read a second view.
A range that spans a shard's months needs nearly every member, and streaming it one member at a time only added allocation churn (about 200MB more RSS on report -p all over a 1GB cache). The streaming reader also reuses one read buffer instead of allocating a fresh one per chunk.
The previous rule also required the range to reach past the shard's newest month, which a range ending now never does, so report -p all still streamed every shard.
Members of a megabyte and more span the 1MB reads, and joining their pieces as Buffers allocated off-heap memory that only a GC returns: report -p all over a 1GB cache peaked about 170MB above main in RSS. Decoding the pieces with a StringDecoder keeps them on the heap and brings every streamed range below main, so the whole-file parse for wide ranges goes again.
iamtoruk
force-pushed
the
perf/bounded-load
branch
from
September 24, 2026 17:39
ef82b1e to
0f0ad9f
Compare
This was referenced Sep 24, 2026
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.
Closes #1504. First stage of bounded cache loading.
What was wrong
A ranged query (
overview -p today,status, the menu bar) loaded every session-cache shard in its month scope whole, then kept only what the range needed. On a heavy corpus that is gigabytes for a few hundred MB of today's data, and it runs out of heap. Shards past V8's string limit (~512 MB) also failed to save silently, so those users re-parsed everything on every run. Codex project discovery loaded the whole results cache just to read project names.What changed
codex-projectsindex written on flush.statusloads once for today and month. The pre-lock view is released before the canonical reload.Verification
Figures, against main, with frozen pricing: 108/108 outputs identical (overview, report, status text/json/menubar-json, sessions, models, export; today, yesterday, week, month, 3 months, all) on a real corpus (5,415 cached sessions), a synthetic heavy corpus and an edge corpus (sessions spanning midnight and three months, moved, shrunk and deleted files, forks, resumed copies, spawn anchors, WSL offline members), under local, Pacific/Kiritimati and America/Adak time zones, fresh and across upgrade and downgrade. Cache members identical, order included. Longer ranges never total less than shorter ones inside them. Serve child through period switches, churn and deletions: identical. Five processes plus a serve child at once, and 30 SIGKILLs mid-run: no damaged shard.
Speed and memory, warm, median of 3 to 5:
overview -p todaystatus --format menubar-jsonreport -p alloverview -p todayreport -p allAt a 1 GB heap cap on the heavy corpus main crashes on
overview -p todayand the menubar payload; this PR passes both.tsc clean, full suite green except
RootDeepProjectLabels.test.tsx, which fails identically on main in this checkout (duplicate React from a symlinked node_modules).test:locks36/36. Seven new tests fail on main's source: stub dedup replay and first-paint order, stub-only eviction on disk, partial-bucket streaming merge and published-name reuse, legacy shard upgrade, codex-projects discovery, stub members equal to a full load.Not covered here
The first run after upgrading still parses a huge legacy shard whole once. Heaps under ~768 MB on the heavy corpus stay out of reach while today's own data is ~430 MB. Per-day pieces, batched detail views and the cold path are the next stages.