Skip to content

Server: Responses and Kimi streams keep memory linear in the reply (a 32k Kimi stream held 9.6 GiB) - #2642

Merged
ashhart merged 7 commits into
mainfrom
lead/stream-memory
Nov 9, 2026
Merged

ashhart merged 7 commits into
mainfrom
lead/stream-memory

Conversation

@ashhart

@ashhart ashhart commented Nov 9, 2026

Copy link
Copy Markdown
Owner

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

  • Responses: each output item (reasoning, text, function-call arguments) grows in one buffer, instead of storing a fresh copy of the item's whole text with every delta.
  • Kimi chat: the native stream parser advances through the new tokens only, instead of re-decoding the whole reply into new lists every chunk.
  • Unchanged paths: Kimi adapters without token bytes and non-chat completions keep the old path. Tool calls still come only from the final whole parse.
  • A fallback: if the incremental Kimi parse meets an id that has no bytes (a padded vocabulary), the stream hands the rest of the reply to the whole parse, which decodes it as before.

Memory

Request arena after the handler, MiB, at 7,500 / 15,000 / 30,000 reasoning tokens (host benchmark, no GPU):

Route Main This PR
Qwen chat (unchanged) 14.0 / 21.1 / 47.9 14.0 / 21.1 / 47.9
Qwen Responses 189.9 / 642.9 / 2,173.2 16.8 / 38.2 / 57.4
Kimi chat 561.3 / 1,896.9 / 9,611.0 9.5 / 21.8 / 49.8

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

  • Every streamed event and whole reply is byte-identical to main's. A wire test runs the original handlers' outputs as an oracle: 84 whole and streamed requests at strides 1, 2 and 7, with Responses reasoning, prose and arguments, Kimi fixtures and stop strings.
  • The incremental Kimi parser equals the unchanged whole parse at every prefix (strides 1, 2, 7), including UTF-8 split at every byte, invalid sequences, repeated channels and malformed controls.
  • New bounds fail on main: a Responses item test (under 4 MiB) and the benchmark's limits (under 80 MiB at 7,500 tokens; main retains 189.9 and 561.3 MiB).

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. Under test-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.
  • Lean: no new findings.

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
ashhart force-pushed the lead/stream-memory branch from 9ddf5cf to 530ff8d Compare November 9, 2026 05:52
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
ashhart force-pushed the lead/stream-memory branch from 530ff8d to e29b26b Compare November 9, 2026 06:40
…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
ashhart force-pushed the lead/stream-memory branch from 80076c9 to 944575d Compare November 9, 2026 07:56
@ashhart
ashhart merged commit f212eea into main Nov 9, 2026
6 of 10 checks passed
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
ashhart deleted the lead/stream-memory branch November 10, 2026 16:35
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.

1 participant