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.
This commit is contained in:
can1357
2026-07-27 20:33:37 +02:00
parent 7db4463da6
commit b459bf3ec6
+4 -2
View File
@@ -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];