From b459bf3ec63eee339c4604fe3fe2a64a02ae2e68 Mon Sep 17 00:00:00 2001 From: can1357 Date: Mon, 27 Jul 2026 20:33:37 +0200 Subject: [PATCH] perf(ai): optimized stream reader buffer handling to eliminate copying - Optimize stream reader buffer handling by avoiding unnecessary copying when the pending buffer is empty. --- packages/ai/src/providers/cursor.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/ai/src/providers/cursor.ts b/packages/ai/src/providers/cursor.ts index c11278e15..35232ce74 100644 --- a/packages/ai/src/providers/cursor.ts +++ b/packages/ai/src/providers/cursor.ts @@ -503,7 +503,7 @@ export const streamCursor: StreamFunction<"cursor-agent"> = ( stream.push({ type: "start", partial: output }); - let pendingBuffer = Buffer.alloc(0); + let pendingBuffer: Buffer = Buffer.alloc(0); let currentTextBlock: (TextContent & { [kStreamingBlockIndex]: number }) | null = null; let currentThinkingBlock: (ThinkingContent & { [kStreamingBlockIndex]: number }) | null = null; let currentToolCall: ToolCallState | null = null; @@ -557,7 +557,9 @@ export const streamCursor: StreamFunction<"cursor-agent"> = ( log?.write(chunk); }); } - pendingBuffer = Buffer.concat([pendingBuffer, chunk]); + // Steady state drains fully per chunk; alias the fresh h2 chunk instead + // of copying it through Buffer.concat (see aws-eventstream.ts). + pendingBuffer = pendingBuffer.length === 0 ? chunk : Buffer.concat([pendingBuffer, chunk]); while (pendingBuffer.length >= 5) { const flags = pendingBuffer[0];