Conversation
Opening a session adds it to historyPrefetchSessions and kicks off
fetchOlderMessagesInBackground, which walked backward through the entire
history unbounded (until hasMoreOlder was false or seq<=1). Because
applyMessages rebuilds the whole messagesMap ({...map}) and re-sorts the
full message array on every applied page, a full prefetch is quadratic in
history length. On web this saturates the main thread: a captured profile
showed ~23s Scripting / 36s with a dominant "evaluate" self-time and heavy
Major GC, while the native mobile client (same account/server) stayed
instant. Long sessions also stayed sluggish on re-entry as their whole
history sat in memory.
Bound the background loop to MAX_PAGES (5 pages ~= 600 messages of scroll
buffer beyond the initial page). The rest of the history still loads on
demand via loadOlderMessages, already wired to FlatList.onEndReached, so
scroll-up is unchanged. Caps prefetch cost and memory at O(budget * n)
instead of O(n^2).
Refs slopus#1453, slopus#1765.
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
f-liva
force-pushed
the
pr/perf-bound-history-prefetch
branch
from
September 16, 2026 08:57
ec24957 to
a053737
Compare
Contributor
Author
|
Superseded by #1797, which reimplements the same 5-page prefetch bound on current |
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
Bound the background history prefetch to a small page budget instead of pulling all older messages into memory.
Why
On the web client, opening a session and returning to it becomes very sluggish on long sessions. A Chrome Performance profile (~36 s capture) shows the main thread saturated by synchronous JS, not rendering:
evaluate16,039 ms (61%), Major GC 1,312 ms (heavy allocation churn)Root cause is O(n²):
fetchOlderMessagesInBackground(sources/sync/sync.ts) ran an unboundedwhile (true)loop pulling every older page for each visible session.applyMessages(sources/sync/storage.ts), which rebuilds the whole map ({ ...messagesMap }) and re-sorts every message (Object.values().sort()) — O(n) per page.evaluatebundle churn).Change
fetchOlderMessagesInBackground: replace the unboundedwhile (true)with a boundedforloop (MAX_PAGES = 5, ~600 messages of buffer). The loop still stops early on!hasMoreOlder, missing session/key, oroldestSeq <= 1. The rest of the history keeps loading on demand vialoadOlderMessages(already wired toFlatList.onEndReached), so nothing is lost — it just isn't all decrypted/sorted up front.Single-file change,
pnpm typecheckclean.Verified
Built and deployed to a self-hosted instance; the open/return freeze on long sessions is gone in day-to-day web use.
Related
Follow-ups (not in this PR)
applyMessagesis still O(n) per call (immutable map copy + full re-sort); this PR reduces how often it is called rather than its per-call cost.fetchSessions) decrypts all sessions' metadata + agentState synchronously on the main thread — a separate hotspot.🤖 Generated with Claude Code