Repository navigation
Server: Responses and Kimi streams keep memory linear in the reply (a 32k Kimi stream held 9.6 GiB) - #2642
Merged
Conversation
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
force-pushed
the
lead/stream-memory
branch
from
November 9, 2026 05:52
9ddf5cf to
530ff8d
Compare
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
force-pushed
the
lead/stream-memory
branch
from
November 9, 2026 06:40
530ff8d to
e29b26b
Compare
…unk at a time A long reasoning reply re-reads the whole visible text on every chunk in several places, so the request thread's work per chunk grows with the reply. zig build bench-stream-scan measures it on the real path: a scripted engine hands 30,000 reasoning tokens, prose and a Qwen XML, GLM, DSML or Kimi call to the chat, Messages and Responses handlers one or two tokens a chunk, a write hook times every frame, and the step prints each chunk's time at 1k to 30k tokens, the whole-text steps' times alone, and the request arena. Built ReleaseSafe as the native server's HTTP side is (-Dstream-scan-optimize for another mode). Under test-server-cpu the same tests run a 300-token reply and check that every route's streamed reasoning, prose and call arguments equal the reply's.
…to the whole parse instead of failing The incremental parse asserted every id has bytes; with a padded vocabulary an id past the specials has none, and the whole parse (main's path) decodes it. The stream now marks itself and every later chunk takes that path.
ashhart
force-pushed
the
lead/stream-memory
branch
from
November 9, 2026 07:56
80076c9 to
944575d
Compare
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
ashhart
added a commit
that referenced
this pull request
Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A long streamed reply on two routes kept a copy of the whole reply for every chunk, so the request's memory grew with the square of the reply's length. Nothing leaked (it was freed when the request ended), but one 32k-token stream held gigabytes, and several at once on a machine near its memory limit would swap.
What changes
Memory
Request arena after the handler, MiB, at 7,500 / 15,000 / 30,000 reasoning tokens (host benchmark, no GPU):
Per-chunk time also falls, µs at the 30k window: Responses 133.85 to 124.72, Kimi 325.81 to 7.35. A whole 32k-token Kimi stream takes 5,518 ms of request-thread time on main and 258 ms here.
Exactness
The benchmark
zig build bench-stream-scan(first commit) drives the real chat, Messages and Responses handlers with a scripted engine: 30,000 reasoning tokens, prose, then a Qwen XML, GLM, DSML or Kimi call, one or two tokens a chunk, timing every frame and reading the request arena. Undertest-server-cpu, the same tests run a 300-token reply and check every route's streamed reasoning, prose and call arguments equal the reply's.Checks
zig build test-server-cpu(macOS): 1,193 passed, 7 skipped.