Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

perf(hooks): split SSE events in one pass over each chunk

Jack Rudenkof217f43f35cd
diff --git a/packages/cli/src/hooks/sse-codec.test.ts b/packages/cli/src/hooks/sse-codec.test.ts
index a46fa571..2d3b3381 100644
--- a/packages/cli/src/hooks/sse-codec.test.ts
+++ b/packages/cli/src/hooks/sse-codec.test.ts
@@ -74,6 +74,45 @@ describe("SseSplitter", () => {
});
});
+describe("SseSplitter: review fixes", () => {
+ const event = (i: number) =>
+ `event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"chunk ${i} of the answer, padded to a typical delta size ......"}}\n\n`;
+
+ it("one 8 MB chunk of 40,000 events splits in linear time", () => {
+ const whole = Array.from({ length: 40_000 }, (_, i) => event(i)).join("");
+ expect(whole.length).toBeGreaterThan(5_000_000);
+ const t0 = performance.now();
+ const events = new SseSplitter().push(bytes(whole));
+ const ms = performance.now() - t0;
+ expect(events.length).toBe(40_000);
+ expect(events.map((e) => e.raw).join("")).toBe(whole);
+ // quadratic: ~5,500 ms measured on Bun 1.4.0; linear: tens of ms
+ expect(ms).toBeLessThan(1_000);
+ });
+
+ it("terminators split across pushes, in every byte position, CRLF and LF mixed", () => {
+ const wire = "event: a\r\ndata: 1\r\n\r\nevent: b\ndata: 2\n\ndata: 3\r\n\ndata: 4\n\r\n\n";
+ const expected = new SseSplitter().push(wire).map((e) => e.raw);
+ for (let cut = 0; cut <= wire.length; cut++) {
+ for (let cut2 = cut; cut2 <= wire.length; cut2 += 3) {
+ const s = new SseSplitter();
+ const got = [
+ ...s.push(wire.slice(0, cut)),
+ ...s.push(wire.slice(cut, cut2)),
+ ...s.push(wire.slice(cut2)),
+ ].map((e) => e.raw);
+ expect(got).toEqual(expected);
+ }
+ }
+ expect(expected).toEqual([
+ "event: a\r\ndata: 1\r\n\r\n",
+ "event: b\ndata: 2\n\n",
+ "data: 3\r\n\n",
+ "data: 4\n\r\n\n",
+ ]);
+ });
+});
+
describe("parseSseFields", () => {
it("joins data lines, strips one leading space, ignores comments", () => {
expect(parseSseFields(": comment\nevent:x\ndata: a\ndata: b\n\n")).toEqual({
diff --git a/packages/cli/src/hooks/sse-codec.ts b/packages/cli/src/hooks/sse-codec.ts
index d8d69a9c..88e5bf0b 100644
--- a/packages/cli/src/hooks/sse-codec.ts
+++ b/packages/cli/src/hooks/sse-codec.ts
@@ -28,18 +28,6 @@ export interface SseEvent {
complete: boolean;
}
-const SEPARATORS = ["\r\n\r\n", "\n\n"] as const;
-
-/** Index and length of the earliest event terminator in `text`, or null. */
-function findTerminator(text: string): { at: number; length: number } | null {
- let best: { at: number; length: number } | null = null;
- for (const sep of SEPARATORS) {
- const at = text.indexOf(sep);
- if (at !== -1 && (best === null || at < best.at)) best = { at, length: sep.length };
- }
- return best;
-}
-
/** Parse the fields of one event's text (terminator optional). */
export function parseSseFields(raw: string): { event: string | null; data: string | null } {
let event: string | null = null;
@@ -60,6 +48,8 @@ export function parseSseFields(raw: string): { event: string | null; data: strin
export class SseSplitter {
private readonly decoder = new TextDecoder("utf-8");
private buffer = "";
+ /** where the next `\n` search resumes: the buffer before it holds no terminator */
+ private scanFrom = 0;
/** Feed one network chunk; returns every event it completed, in order. */
push(chunk: Uint8Array | string): SseEvent[] {
@@ -74,6 +64,7 @@ export class SseSplitter {
if (this.buffer.length > 0) {
const raw = this.buffer;
this.buffer = "";
+ this.scanFrom = 0;
events.push({ raw, ...parseSseFields(raw), complete: false });
}
return events;
@@ -84,15 +75,53 @@ export class SseSplitter {
return this.buffer.length;
}
+ /**
+ * Cut every complete event off the buffer, in ONE pass over the text added since the
+ * last call, and slice the buffer once. A terminator is the earliest `\n\n`
+ * or `\r\n\r\n`; both contain a `\n` followed by `\n` or `\r\n`, so only `\n`
+ * positions are visited. A `\n` too close to the end to decide is resumed from on
+ * the next push. Searching both separators over the whole remainder once per event
+ * was quadratic in the size of one chunk: 5.5 s for 8 MB of deltas.
+ */
private drain(): SseEvent[] {
const events: SseEvent[] = [];
- for (;;) {
- const term = findTerminator(this.buffer);
- if (!term) break;
- const raw = this.buffer.slice(0, term.at + term.length);
- this.buffer = this.buffer.slice(term.at + term.length);
+ const buf = this.buffer;
+ let start = 0;
+ let resume = -1;
+ let i = buf.indexOf("\n", this.scanFrom);
+ while (i !== -1) {
+ const next = buf[i + 1];
+ if (next === undefined) {
+ resume = i;
+ break;
+ }
+ let at = -1;
+ let length = 0;
+ if (next === "\n") {
+ at = i;
+ length = 2;
+ } else if (next === "\r" && i - 1 >= start && buf[i - 1] === "\r") {
+ const after = buf[i + 2];
+ if (after === undefined) {
+ resume = i;
+ break;
+ }
+ if (after === "\n") {
+ at = i - 1;
+ length = 4;
+ }
+ }
+ if (at === -1) {
+ i = buf.indexOf("\n", i + 1);
+ continue;
+ }
+ const raw = buf.slice(start, at + length);
events.push({ raw, ...parseSseFields(raw), complete: true });
+ start = at + length;
+ i = buf.indexOf("\n", start);
}
+ this.buffer = start === 0 ? buf : buf.slice(start);
+ this.scanFrom = resume === -1 ? this.buffer.length : resume - start;
return events;
}
}