The best way to control Claude Code, Codex and more from any computer or phone
perf(server): stop remapping every thread on each thread event (#13720)
Co-authored-by: Claude <noreply@anthropic.com>
Theo Browne committed
Sep 25, 2026 at 19:11 UTC
c216ba4dade683bf3a7c96bf63b33a85afc1a16c
1 file changed
+22
-6
apps/server/src/orchestration/projector.ts
+22
-6
@@ -117,12 +117,26 @@ function settledTurnStateForSessionStatus(
117
}
118
}
119
120
+// Runs for every thread event (including streaming deltas) against every
121
+// thread the server has ever seen, so copy the array rather than map it.
122
function updateThread(
123
threads: ReadonlyArray<OrchestrationThread>,
124
threadId: ThreadId,
125
patch: ThreadPatch,
124
-): OrchestrationThread[] {
125
- return threads.map((thread) => (thread.id === threadId ? { ...thread, ...patch } : thread));
126
+): ReadonlyArray<OrchestrationThread> {
127
+ const index = threads.findIndex((thread) => thread.id === threadId);
128
+ return index === -1 ? threads : patchThreadAt(threads, index, patch);
129
+}
130
+
131
+/** For callers that already located the thread and must not scan again. */
132
+function patchThreadAt(
133
+ threads: ReadonlyArray<OrchestrationThread>,
134
+ index: number,
135
+ patch: ThreadPatch,
136
+): ReadonlyArray<OrchestrationThread> {
137
+ const next = threads.slice();
138
+ next[index] = { ...threads[index]!, ...patch };
139
+ return next;
140
}
141
142
/** Patch that swaps a thread's links and re-derives the legacy single-PR field from them. */
@@ -767,7 +781,8 @@ export function projectEvent(
781
event.type,
782
"payload",
783
);
770
- const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
784
+ const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
785
+ const thread = nextBase.threads[threadIndex];
786
if (!thread) {
787
return nextBase;
788
}
@@ -815,7 +830,7 @@ export function projectEvent(
830
831
return {
832
...nextBase,
818
- threads: updateThread(nextBase.threads, payload.threadId, {
833
+ threads: patchThreadAt(nextBase.threads, threadIndex, {
834
messages: cappedMessages,
835
updatedAt: event.occurredAt,
836
}),
@@ -1055,7 +1070,8 @@ export function projectEvent(
1070
"payload",
1071
).pipe(
1072
Effect.map((payload) => {
1058
- const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
1073
+ const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
1074
+ const thread = nextBase.threads[threadIndex];
1075
if (!thread) {
1076
return nextBase;
1077
}
@@ -1069,7 +1085,7 @@ export function projectEvent(
1085
1086
return {
1087
...nextBase,
1072
- threads: updateThread(nextBase.threads, payload.threadId, {
1088
+ threads: patchThreadAt(nextBase.threads, threadIndex, {
1089
activities,
1090
updatedAt: event.occurredAt,
1091
}),