Skip to content

Commit f7d1210

Browse files
authored
perf(sse): split only the newly decoded text in stream line readers (#10118)
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.
1 parent 165c3db commit f7d1210

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎apps/sim/executor/handlers/mothership/handler.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,9 @@ async function readMothershipExecuteResponse(
571571
const { done, value } = await reader.read()
572572
if (done) break
573573

574-
buffer += decoder.decode(value, { stream: true })
575-
const lines = buffer.split('\n')
574+
// `buffer` never holds a newline, so only the new text needs splitting.
575+
const lines = decoder.decode(value, { stream: true }).split('\n')
576+
lines[0] = buffer + lines[0]
576577
buffer = lines.pop() ?? ''
577578
for (const line of lines) {
578579
await processLine(line)
@@ -700,8 +701,9 @@ function createMothershipStreamingExecution(
700701
if (cancelled) return
701702
if (done) break
702703

703-
buffer += decoder.decode(value, { stream: true })
704-
const lines = buffer.split('\n')
704+
// `buffer` never holds a newline, so only the new text needs splitting.
705+
const lines = decoder.decode(value, { stream: true }).split('\n')
706+
lines[0] = buffer + lines[0]
705707
buffer = lines.pop() ?? ''
706708
for (const line of lines) {
707709
await processLine(line)

‎apps/sim/lib/core/utils/sse.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ export async function readSSELines(source: SSESource, options: ReadSSELinesOptio
166166

167167
const { done, value } = await readChunk()
168168

169-
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true })
170-
const lines = buffer.split('\n')
169+
// `buffer` never holds a newline, so split only the new text: re-splitting
170+
// the whole pending line on every chunk is quadratic in the event size.
171+
const lines = (done ? decoder.decode() : decoder.decode(value, { stream: true })).split('\n')
172+
lines[0] = buffer + lines[0]
171173
buffer = done ? '' : (lines.pop() ?? '')
172174

173175
for (const rawLine of lines) {

‎packages/sim-cli/src/commands/protocol/workflow-run-follow.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ async function* sseData(body: ReadableStream<Uint8Array>): AsyncGenerator<string
272272
try {
273273
while (true) {
274274
const { done, value } = await reader.read()
275-
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true })
276-
277-
const lines = buffer.split('\n')
275+
// `buffer` never holds a newline, so only the new text needs splitting.
276+
const lines = (done ? decoder.decode() : decoder.decode(value, { stream: true })).split('\n')
277+
lines[0] = buffer + lines[0]
278278
buffer = done ? '' : (lines.pop() ?? '')
279279

280280
for (const rawLine of lines) {

‎packages/sim-cli/src/http/ndjson.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export async function* readNdjson(
2929
try {
3030
while (true) {
3131
const { done, value } = await reader.read()
32-
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true })
33-
34-
const lines = buffer.split('\n')
32+
// `buffer` never holds a newline, so only the new text needs splitting.
33+
const lines = (done ? decoder.decode() : decoder.decode(value, { stream: true })).split('\n')
34+
lines[0] = buffer + lines[0]
3535
buffer = done ? '' : (lines.pop() ?? '')
3636
for (const line of lines) {
3737
const event = parse(line)

0 commit comments

Comments
 (0)