From 825d90dbabe90f34af5400c8390483c4a96411ff Mon Sep 17 00:00:00 2001 From: b84330808 Date: Mon, 16 Nov 2026 11:11:34 +0900 Subject: [PATCH] perf(executor): stop re-parsing the whole structured output on every streamed token When a chat selects a response-format field, StreamingResponseFormatProcessor re-ran JSON.parse on the whole buffer for every token delta, then scanned it twice more with brace-count regexes, so CPU per response grew with the square of the output length. Text can only parse to an object once it ends in `}` or `]`, so parse only then. The brace-count branch re-parsed the same `buffer.trim()` that had just failed or yielded a non-object, so it could never return a result and is removed. --- apps/sim/executor/utils.ts | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/apps/sim/executor/utils.ts b/apps/sim/executor/utils.ts index 11deda3f9a0..32e2763b3a6 100644 --- a/apps/sim/executor/utils.ts +++ b/apps/sim/executor/utils.ts @@ -50,6 +50,7 @@ export class StreamingResponseFormatProcessor { ): ReadableStream { let buffer = '' let hasProcessedComplete = false + let endsWithBracket = false const self = this @@ -73,7 +74,12 @@ export class StreamingResponseFormatProcessor { const chunk = decoder.decode(value, { stream: true }) buffer += chunk - if (!hasProcessedComplete) { + // Only text ending in `}` or `]` can parse to an object, so skip re-parsing + // the whole buffer on every other token: that made each stream quadratic. + const trimmedChunk = chunk.trimEnd() + if (trimmedChunk) endsWithBracket = /[}\]]$/.test(trimmedChunk) + + if (!hasProcessedComplete && endsWithBracket) { const processedChunk = self.processStreamingChunk(buffer, selectedFields) if (processedChunk) { @@ -114,32 +120,6 @@ export class StreamingResponseFormatProcessor { } } catch {} - const openBraces = (buffer.match(/\{/g) || []).length - const closeBraces = (buffer.match(/\}/g) || []).length - - if (openBraces > 0 && openBraces === closeBraces) { - try { - const parsed = JSON.parse(buffer.trim()) - if (typeof parsed === 'object' && parsed !== null) { - const results: string[] = [] - for (const field of selectedFields) { - if (field in parsed) { - const value = parsed[field] - const formattedValue = typeof value === 'string' ? value : JSON.stringify(value) - results.push(formattedValue) - } - } - - if (results.length > 0) { - const result = results.join('\n') - return result - } - - return null - } - } catch {} - } - return null }