Repository navigation
fix(template): append in place when a namespace string accumulates - #973
Merged
Merged
Conversation
Closed
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 |
6 of 8 tasks
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
8 of 9 tasks
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).
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.
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).Ctxkeeps 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 andsafehandling 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: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-zigwith 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
appending to a long string in a namespace stays linear in memoryrenders 600 messages of 1 KiB with+and with~inside a 24 MiBFixedBufferAllocator. 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 inbenchmark_server_test,benchmark_cli_testandbenchmark_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.