Skip to content
TypeScript

perf(server): find in a long thread no longer re-parses every message on each keystroke (#20801)

Aaron Queenf3bb914efbe0
diff --git a/apps/server/src/orchestration-v2/ThreadFind.test.ts b/apps/server/src/orchestration-v2/ThreadFind.test.ts
index a49fb3925cb..0121227f6e8 100644
--- a/apps/server/src/orchestration-v2/ThreadFind.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadFind.test.ts
@@ -456,6 +456,28 @@ describe("V2 thread find", () => {
}).pipe(Effect.provide(layerTest)),
);
+ it.effect("reuses parses across queries in a thread of a few hundred messages", () =>
+ Effect.gen(function* () {
+ const projection = yield* setup;
+ const total = 600;
+ yield* putItems(
+ Array.from({ length: total }, (_, i) => item(`long:${i}`, i + 1, `message ${i} needle`)),
+ );
+ const parse = vi.spyOn(ThreadFindText, "searchableMessageSegments");
+ try {
+ const first = yield* projection.searchThread({ threadId, query: "needle" });
+ assert.equal(first.totalMatches, total);
+ assert.equal(parse.mock.calls.length, total);
+ parse.mockClear();
+ const typed = yield* projection.searchThread({ threadId, query: "message 5" });
+ assert.equal(typed.totalMatches, 111);
+ assert.equal(parse.mock.calls.length, 0);
+ } finally {
+ parse.mockRestore();
+ }
+ }).pipe(Effect.provide(layerTest)),
+ );
+
it.effect("searches visible rendered messages and plans in canonical item order", () =>
Effect.gen(function* () {
const projection = yield* setup;
diff --git a/apps/server/src/orchestration-v2/ThreadFind.ts b/apps/server/src/orchestration-v2/ThreadFind.ts
index 10d0b5cecac..288737471e0 100644
--- a/apps/server/src/orchestration-v2/ThreadFind.ts
+++ b/apps/server/src/orchestration-v2/ThreadFind.ts
@@ -263,8 +263,10 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
});
});
});
+ // Each scan visits every message, so a cache smaller than the thread is cycled through and
+ // never hits. Real threads reach a few thousand messages of a few hundred characters.
const textCache = yield* Cache.make({
- capacity: 512,
+ capacity: 8192,
timeToLive: "1 minute",
lookup: (key: TextKey) => Effect.sync(() => parseText(key)),
});