Skip to content

perf(sync): bound background history prefetch to a small page budget - #1766

Closed
f-liva wants to merge 1 commit into
slopus:mainfrom
f-liva:pr/perf-bound-history-prefetch
Closed

f-liva wants to merge 1 commit into
slopus:mainfrom
f-liva:pr/perf-bound-history-prefetch

Conversation

@f-liva

@f-liva f-liva commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

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:

  • Scripting 23,374 ms / 36,350 ms (~64%); Rendering 111 ms, Painting 79 ms (negligible)
  • Bottom-Up self time: evaluate 16,039 ms (61%), Major GC 1,312 ms (heavy allocation churn)

Root cause is O(n²):

  • fetchOlderMessagesInBackground (sources/sync/sync.ts) ran an unbounded while (true) loop pulling every older page for each visible session.
  • Each applied page calls applyMessages (sources/sync/storage.ts), which rebuilds the whole map ({ ...messagesMap }) and re-sorts every message (Object.values().sort()) — O(n) per page.
  • Unbounded pages × O(n)-per-page = O(n²) + constant Major GC → main-thread saturation on web (native is fine: native crypto, no JS evaluate bundle churn).

Change

fetchOlderMessagesInBackground: replace the unbounded while (true) with a bounded for loop (MAX_PAGES = 5, ~600 messages of buffer). The loop still stops early on !hasMoreOlder, missing session/key, or oldestSeq <= 1. The rest of the history keeps loading on demand via loadOlderMessages (already wired to FlatList.onEndReached), so nothing is lost — it just isn't all decrypted/sorted up front.

Single-file change, pnpm typecheck clean.

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)

  • applyMessages is 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.
  • Session list load (fetchSessions) decrypts all sessions' metadata + agentState synchronously on the main thread — a separate hotspot.

🤖 Generated with Claude Code

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

f-liva commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1797, which reimplements the same 5-page prefetch bound on current main (upstream refactored the prefetch loop into fetchOlderMessagesInBackground since this PR was written against the older tree).

@f-liva f-liva closed this Sep 16, 2026
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