From 4c08e7fc0083c43682bd31a66ddaafef2f9a3bdd Mon Sep 17 00:00:00 2001 From: b84330808 Date: Mon, 16 Nov 2026 11:09:30 +0900 Subject: [PATCH] perf(sse): split only the newly decoded text in stream line readers The SSE and NDJSON line readers appended each decoded chunk to the pending line and re-split the whole buffer, so one large event (a block output of a few MB in the run stream) was rescanned and re-flattened on every chunk: quadratic in the event size. The pending remainder never holds a newline, so splitting just the new text and prefixing the first piece gives the same lines. --- apps/sim/executor/handlers/mothership/handler.ts | 10 ++++++---- apps/sim/lib/core/utils/sse.ts | 6 ++++-- .../src/commands/protocol/workflow-run-follow.ts | 6 +++--- packages/sim-cli/src/http/ndjson.ts | 6 +++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/apps/sim/executor/handlers/mothership/handler.ts b/apps/sim/executor/handlers/mothership/handler.ts index 6c2cd963afc..c746f4e35f8 100644 --- a/apps/sim/executor/handlers/mothership/handler.ts +++ b/apps/sim/executor/handlers/mothership/handler.ts @@ -571,8 +571,9 @@ async function readMothershipExecuteResponse( const { done, value } = await reader.read() if (done) break - buffer += decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so only the new text needs splitting. + const lines = decoder.decode(value, { stream: true }).split('\n') + lines[0] = buffer + lines[0] buffer = lines.pop() ?? '' for (const line of lines) { await processLine(line) @@ -700,8 +701,9 @@ function createMothershipStreamingExecution( if (cancelled) return if (done) break - buffer += decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so only the new text needs splitting. + const lines = decoder.decode(value, { stream: true }).split('\n') + lines[0] = buffer + lines[0] buffer = lines.pop() ?? '' for (const line of lines) { await processLine(line) diff --git a/apps/sim/lib/core/utils/sse.ts b/apps/sim/lib/core/utils/sse.ts index b68af4bf783..871ad21b56c 100644 --- a/apps/sim/lib/core/utils/sse.ts +++ b/apps/sim/lib/core/utils/sse.ts @@ -166,8 +166,10 @@ export async function readSSELines(source: SSESource, options: ReadSSELinesOptio const { done, value } = await readChunk() - buffer += done ? decoder.decode() : decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') + // `buffer` never holds a newline, so split only the new text: re-splitting + // the whole pending line on every chunk is quadratic in the event size. + const lines = (done ? decoder.decode() : decoder.decode(value, { stream: true })).split('\n') + lines[0] = buffer + lines[0] buffer = done ? '' : (lines.pop() ?? '') for (const rawLine of lines) { diff --git a/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts b/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts index ed43a1fb47f..c2eaac496d2 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-run-follow.ts @@ -272,9 +272,9 @@ async function* sseData(body: ReadableStream): AsyncGenerator