perf(server): find in a long thread no longer re-parses every message on each keystroke #20801
main fromrefs/pull/20801/head·merged by juliusmarminge View on GitHubThe server's find call is about 6x faster after the first keystroke on long threads: about 600–760 ms per keystroke before, about 100–110 ms after, on the three largest threads in a real database (2,000–2,700 searchable messages each).
Problem#
Each find scan visits every message and looks its parsed text up in a cache that held 512 entries. A thread with more than 512 distinct messages is walked in order, so each lookup evicts an entry the same scan needs next, and nothing is ever found in the cache. Every keystroke (each is a new query) and every next/previous match press while the thread is streaming re-parses the whole thread.
Change#
Raise the parsed-text cache capacity from 512 to 8192 (ThreadFind.ts), with a comment saying why it must exceed the largest thread. Add a regression test: on a 600-message thread, a second query that differs from the first re-parses nothing (600 parses on the second query before the change, 0 after).
Scope and approval#
Reported in #20790 (filed through the triage form). A one-constant fix for a performance defect, with a test that fails without it. No output changes: match counts and positions are identical between the two builds in every measured case.
Verification#
Read-only snapshot of a real 15 GB database, three largest threads, ThreadFind called in process (so these are server times, not measured in the UI). Before = upstream/main, after = this branch. Five interleaved pairs of separate processes (order alternated), host load 3.4–5.3; queries are 3–6 character prefixes of words in each thread, every typing call a new query. Median per call:
| Thread (searchable messages) | Scenario | Before | After | Benefit |
|---|---|---|---|---|
| 2,028 | typing, after the first keystroke | 600 ms | 104 ms | 5.8x |
| 2,028 | next match while the thread streams | 596 ms | 105 ms | 5.7x |
| 2,726 | typing, after the first keystroke | 751 ms | 112 ms | 6.7x |
| 2,726 | next match while the thread streams | 764 ms | 107 ms | 7.1x |
| 2,359 | typing, after the first keystroke | 731 ms | 103 ms | 7.1x |
| 2,359 | next match while the thread streams | 732 ms | 99 ms | 7.4x |
| all three | next match on a quiet thread (cached result) | 0.2–0.3 ms | 0.2 ms | no clear change |
- In a separate run of the same scenarios, the longest uninterrupted event-loop gap was about 50 ms on two of the threads in both builds. On the first thread the medians after the change were 69 ms (typing) and 81 ms (next match while streaming) against 62 and 58 ms before (ranges 61–86 ms against 57–69 ms, five runs each), so possibly slightly longer. The change shortens how long a find call occupies the server (table above), not its longest single block. That block is consistent with the index read, which this PR does not touch; this run did not isolate it.
- The first keystroke of a find session is unchanged (about 830 ms before, 820–930 ms after): the cache starts empty. Entries expire one minute after they were parsed, whether or not they were read since, so a session longer than a minute pays one full parse again.
- Heap after typing grew by about 4–9 MB more than before on these threads (0.6–0.8 M characters of text each). The cache is shared across threads and bounded by entry count, so the worst case is larger: entries hold their text and parsed segments, and 8192 entries of large messages could reach hundreds of MB. Expired entries are only dropped when their key is read or newer entries push them out.
- Threads above 8192 messages still cycle the cache. The separate cache for texts over 32,768 characters keeps its capacity of 32.
- Check:
vp test run apps/server/src/orchestration-v2/ThreadFind.test.ts(24 tests pass); the new test fails on the old capacity with "expected 600 to equal 0". Not checked: UI-side effect of the shorter server time, other machines or data, Bun.
Claude Sonnet 5.5 in Claude Code, run through T3 Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Path: .coderabbit.config.ts
- Review profile: CHILL
- Plan: Advanced
- Run ID:
0a23a6a9-6a38-4f32-90a2-47c553870287
📥 Commits
-
Reviewing files that changed from the base of the PR and between 144aae4d4ecd4f1ba803f7b75cfc9379b4f44957 and ea7b7cf1c9bb0629ecef985a9a637ef56ae2253c.
📒 Files selected for processing: 2
-
apps/server/src/orchestration-v2/ThreadFind.test.tsapps/server/src/orchestration-v2/ThreadFind.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
📝 Walkthrough
-
The thread-search text-parse cache capacity increases from 512 to 8192 entries. A test verifies that two searches across 600 messages reuse parsed message segments. The one-minute cache TTL and lookup behavior remain unchanged.
Changes: Thread search parse cache
Layer / File(s) Summary Cache capacity and reuse
apps/server/src/orchestration-v2/ThreadFind.ts,apps/server/src/orchestration-v2/ThreadFind.test.tsThe cache capacity increases to 8192 entries. A test verifies that the first search parses 600 messages and the second search finds 111 matches without additional parsing. Priority: ➖ Normal
Estimated code review effort: 1 (Trivial) | ~5 minutes
Change: Bug fix
Suggested reviewers:
noojuno
### Architecture SummaryMerge Risk: ⚪ Minimal · up to
ea7b7-
The change trades a bounded amount of additional cache memory for faster repeated searches. No actionable merge-blocking risk is established.
Architecture risk: 🔵 Low · up to
ea7b7The change affects 1 system.
Changed systems:
apps/serverArchitecture concerns No architecture-level concerns identified.
Review details
-
Systems and components
- observed — apps/server (service) was modified; 2 changed files map to changed impact.
Before / after behavior
- observed — Modified behavior in apps/server/src/orchestration-v2/ThreadFind.test.ts: Adds coverage for parse reuse in a thread of 600 messages. The first
"needle"search finds 600 matches and parses each message; after clearing the spy, searching for"message 5"finds 111 matches with no additional parsing. - observed — Modified behavior in apps/server/src/orchestration-v2/ThreadFind.ts: The text-parse cache capacity increases from 512 to 8192; its lookup and one-minute TTL remain unchanged.
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly identifies the server performance fix and its effect on long-thread searches. It is concise and uses the repository's conventional commit format. Description check The description is complete and directly addresses the problem, change, scope and approval rationale, verification results, limitations, performance impact, memory impact, and agent usage. It includes… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Description check
-
Explanation
The description is complete and directly addresses the problem, change, scope and approval rationale, verification results, limitations, performance impact, memory impact, and agent usage. It includes focused test results and observed measurements.
-
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
juliusmarminge merged thisf3bb914
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Approved at
ea7b7cfMacroscope's review found this PR approvable — This is a narrowly scoped server performance fix: a bounded parsed-text cache is enlarged so long-thread searches reuse existing parses, with a regression test covering the behavior. Existing search semantics, APIs, and large-message cache limits remain unchanged.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).