Skip to content

Avoid rescanning SSE delimiter buffers#1913

Open
camdencheek wants to merge 1 commit into
openai:mainfrom
camdencheek:master
Open

Avoid rescanning SSE delimiter buffers#1913
camdencheek wants to merge 1 commit into
openai:mainfrom
camdencheek:master

Conversation

@camdencheek

@camdencheek camdencheek commented Jun 2, 2026

Copy link
Copy Markdown
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Modify iterSSEChunks to skip already-scanned bytes so streaming in small chunks does not cause findDoubleNewlineIndex to get called with more data than is necessary.

Additional context & links

In many of our profiles, findDoubleNewlineIndex was consuming a large amount of CPU. In some cases, even pushing our event loop utilization to 100%. This was surprising to me since that should be a pretty cheap operation.

It seems the issue is not findDoubleNewlineIndex itself, but rather that it is re-scanning bytes that it has already checked. Whenever a network chunk comes in, it gets appended to data then that full buffer is searched for a double newline. This means that, in the pathological case of streaming one byte at a time, finding the double newline scans 1 + 2 + ... + n = n * (n + 1) / 2 bytes.

When we measured this in our service, roughly 95% of the bytes scanned were redundantly scanned.

This fixes the issue by skipping the prefix we know can't contain any double newline (or partial double newline) and only scanning new bytes. We've patched this on our end, and it dropped byte scans here by 95% in our production env (as expected).

screenshot_2026-06-02_09-27-42

@camdencheek camdencheek requested a review from a team as a code owner June 2, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants