Skip to content

Split streamed lines - #43

Open
vincent0426 wants to merge 1 commit into
basetenlabs:mainfrom
vincent0426:fix/stream-lines-linear
Open

vincent0426 wants to merge 1 commit into
basetenlabs:mainfrom
vincent0426:fix/stream-lines-linear

Conversation

@vincent0426

Copy link
Copy Markdown

🚀 What

stream_logs got slower the longer a line was: each chunk rescanned the whole line so far, so one 8 MB line took ~26s on a live sandbox.

💻 How

response_lines resplit the whole buffer on every chunk. It now checks only the new chunk for \n and splits the buffer only when one is found.

🔬 Testing

  • Splitting one long line in 16 KB chunks:

    Line Before After
    1 MB 0.38s 0.009s
    2 MB 1.08s 0.017s
    4 MB 4.25s 0.034s
    8 MB 16.9s 0.068s
  • All existing tests pass; poe lint and poe typecheck are clean.

also tried splitting only the new chunk instead of the whole buffer. It saves ~60ms on an 8 MB line (0.068s → 0.006s) but needs more code to handle \r\n split across chunks, so I kept the simpler change.

response_lines re-split the whole pending buffer on every chunk, so a
long line arriving in many chunks took quadratic time: an 8 MB line in
16 KB chunks took about 17s to split, and about 26s through
stream_logs on a live sandbox. Lines without a line ending are common
in process output, such as progress bars, minified JSON and base64.

The buffer is now split only when a chunk contains \n, the only way a
line can end, so an unfinished line is not rescanned.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 20:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues with the streamed-log processing optimization.

No open issues 🎉

What changed in this PR

Improves streamed log processing by avoiding repeated scans of incomplete lines in the synchronous and asynchronous response readers.

Changes:

  • Splits the accumulated buffer only when newly decoded text contains a newline.
  • Adds a regression case for a long line spanning many chunks, with \r\n split across chunks.
File Description
tests/​sandbox/​test_common.py Adds coverage for long chunked lines and split CRLF endings.
baseten/​sandbox/​_common.py Avoids unnecessary buffer splitting in both response readers.

Review effort: Balanced

This branch has not been deployed

No deployments
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