Skip to content

fix(template): append in place when a namespace string accumulates - #973

Merged
ashhart merged 2 commits into
ashhart:mainfrom
nmcbride:template-append-linear
Oct 18, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
nmcbride:template-append-linear

Conversation

@nmcbride

@nmcbride nmcbride commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What changes

String + and ~ in the template evaluator copied both operands into a new arena slice on every evaluation. Chat templates build the prompt with {% set ns.out = ns.out + ... %}, so each append copied the whole prompt so far and nothing was freed until the render ended: memory and time grow with appends x output size. A long agent conversation reaches gigabytes in one render, and a chat request renders more than once (prompt, history, shared-prefix probe).

Ctx keeps one append buffer. When the left operand of + or ~ is that buffer's whole content, the right operand is appended in place; any other left string of 4 KiB or more is copied once into a new buffer of exactly its size, so a one-off concatenation allocates what it did before. A full buffer grows into a new allocation and the old one is left untouched, so earlier values that hold slices of it stay valid. Shorter strings concatenate as before. Escaping and safe handling are unchanged.

Limits: only one buffer is tracked, so a template that grows two long strings in alternation keeps the old cost for one of them, and prepending (ns.out = x + ns.out) still copies as before. Superseded buffers stay allocated until the render ends, about twice the final string, which is what keeps earlier values' slices valid.

Receipt

Base: 0a591781d (main). Zig 0.17.0, Linux x86_64 host.

Render of a synthetic conversation (user, assistant tool-call and tool turns of about 1 KiB each) with the DeepSeek-V4.1-Flash chat template, enable_thinking=true, ReleaseSafe, bytes allocated counted around the render:

Messages Output Before After
100 113 KB 32 MB, 15 ms 1 MB, 1 ms
250 283 KB 213 MB, 89 ms 3 MB, 2 ms
500 566 KB 908 MB, 352 ms 10 MB, 6 ms
1000 1.13 MB 3811 MB, 1259 ms 28 MB, 17 ms

Rendered output is byte-identical before and after at every size, and for two real 997-message agent requests (one with image parts).

On a served DeepSeek-V4.1-Flash TP2 pair (2x GB10, unified memory, bertholomus/TensorFold deepseek-v41-zig with the same change), a cold 341K-token, 997-message request raised the rank-0 server from 2.3 GB to over 10 GB within seconds and the host's OOM guard stopped it; with this change the same request peaks at 3.7 GB and completes.

Tests

  • New: appending to a long string in a namespace stays linear in memory renders 600 messages of 1 KiB with + and with ~ inside a 24 MiB FixedBufferAllocator. Unpatched: 13 passed, 1 failed (OutOfMemory). Patched: 14 passed.
  • zig test -lc zig/src/core/template/tests.zig: 14 passed, 0 skipped, 0 failed.
  • zig build test --summary all: 377 of 392 passed, 1 skipped, 14 failed; identical on unpatched main (the 14 are in benchmark_server_test, benchmark_cli_test and benchmark_run_test, which spawn child processes on this host).
  • python3 tools/zig/lean_check.py: 517 problems before and after (zero added).

Not run: macOS and Metal builds and their host suite; the change is in shared host code with no platform paths.

@ashhart ashhart mentioned this pull request Oct 17, 2026
@ashhart
ashhart merged commit 266dc43 into ashhart:main Oct 18, 2026
@ashhart

ashhart commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Thank you, @nmcbride, for the bounded long-history test and exact-size correction.

We merged both commits for 1.1.0 in 266dc43. Concatenation appends in place only when its left slice is the tracked buffer's whole current content. Growth keeps older slices alive in the render arena and preserves conversion and escaping order.

The 600-message fixture compares both operators under a 24 MiB allocator, targeting the previous copy-per-append behaviour.

Posted by Ash's AI Assistant Synthena

BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…rows in one buffer, so long histories render in linear memory
ashhart pushed a commit that referenced this pull request Oct 24, 2026
…he caller's

renderProvenanceGlobals built its scratch arena on gpa and copied the text and
spans into gpa before freeing it. The server passes its per-connection request
arena there (hf_text.zig renderFn), and an arena takes back only its most recent
allocation, so each render's scratch stayed until the reply ended. A chat
request renders the conversation two or three times (prompt.zig prepare).

The scratch arena now sits on the allocator the template was compiled with and
is freed when the render returns; gpa receives only the text, the spans and a
failure's message. The server compiles its templates on its general-purpose
allocator (hf_text.zig load), which is thread-safe. Callers that compile and
render on one allocator, renderChat among them, see no change, so the namespace
test from #973 still bounds the render's scratch.

A new test renders 64 messages of 64 KiB, from a template compiled on the
testing allocator, into an allocator 2.5 times the size of the text. On main it
fails with OutOfMemory; now the text equals the expected bytes and the allocator
holds only the text and the spans.

By Robert Wojciechowski (GitHub @wojo, X @rwojo).
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