Skip to content
TypeScript

perf(server): stop remapping every thread on each thread event (#13720)

Theo Brownec216ba4dade6
diff --git a/apps/server/src/orchestration/projector.ts b/apps/server/src/orchestration/projector.ts
index 85d9db3fdfe..dd418b95b9b 100644
--- a/apps/server/src/orchestration/projector.ts
+++ b/apps/server/src/orchestration/projector.ts
@@ -117,12 +117,26 @@ function settledTurnStateForSessionStatus(
}
}
+// Runs for every thread event (including streaming deltas) against every
+// thread the server has ever seen, so copy the array rather than map it.
function updateThread(
threads: ReadonlyArray<OrchestrationThread>,
threadId: ThreadId,
patch: ThreadPatch,
-): OrchestrationThread[] {
- return threads.map((thread) => (thread.id === threadId ? { ...thread, ...patch } : thread));
+): ReadonlyArray<OrchestrationThread> {
+ const index = threads.findIndex((thread) => thread.id === threadId);
+ return index === -1 ? threads : patchThreadAt(threads, index, patch);
+}
+
+/** For callers that already located the thread and must not scan again. */
+function patchThreadAt(
+ threads: ReadonlyArray<OrchestrationThread>,
+ index: number,
+ patch: ThreadPatch,
+): ReadonlyArray<OrchestrationThread> {
+ const next = threads.slice();
+ next[index] = { ...threads[index]!, ...patch };
+ return next;
}
/** Patch that swaps a thread's links and re-derives the legacy single-PR field from them. */
@@ -767,7 +781,8 @@ export function projectEvent(
event.type,
"payload",
);
- const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
+ const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
+ const thread = nextBase.threads[threadIndex];
if (!thread) {
return nextBase;
}
@@ -815,7 +830,7 @@ export function projectEvent(
return {
...nextBase,
- threads: updateThread(nextBase.threads, payload.threadId, {
+ threads: patchThreadAt(nextBase.threads, threadIndex, {
messages: cappedMessages,
updatedAt: event.occurredAt,
}),
@@ -1055,7 +1070,8 @@ export function projectEvent(
"payload",
).pipe(
Effect.map((payload) => {
- const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
+ const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
+ const thread = nextBase.threads[threadIndex];
if (!thread) {
return nextBase;
}
@@ -1069,7 +1085,7 @@ export function projectEvent(
return {
...nextBase,
- threads: updateThread(nextBase.threads, payload.threadId, {
+ threads: patchThreadAt(nextBase.threads, threadIndex, {
activities,
updatedAt: event.occurredAt,
}),