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:
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user