Repository navigation
grammar: compile off the lock, wait at most 2,000 ms, run at most 4 at once - #3231
Merged
Merged
Conversation
ashhart
force-pushed
the
lead/grammar-compile-bound
branch
2 times, most recently
from
November 18, 2026 14:35
3be8bcb to
5bc8588
Compare
…t once A grammar request compiled under the server-wide grammar lock with no time bound. xgrammar's compile time grows faster than the square of an object's optional properties, so one such schema held every other grammar request, cached ones included, for as long as it took. A 207 KB schema takes minutes. tf_grammar.cc now hands each compile to tfc::Cache in tf_compile.h. A hit among the last 8 grammars that finished in time is served under its lock. A miss runs on its own thread, with the 8 MiB stack the request threads have, against the same long-lived xgrammar compiler, options and caches as before. Identical pending requests share one compile. At most 4 compiles run at once, and a request that finds all 4 busy waits for one. A request waits at most 2,000 ms in all, then gets the 400 naming the limit. xgrammar has no point at which a compile can be stopped. An abandoned compile runs to the end in the background and keeps its slot until it returns. The grammar it finishes goes into xgrammar's cache but never into the in-time cache, so the same request sent after that is served. The Zig caller drops its compile mutex. The C ABI is unchanged apart from tfg_compiling, a slot count for tests, and an older library still compiles without the time limit. A grammar that compiles within the limit is the one main compiles, from the same compiler object, calls and options. Masks over 14 grammars in all six formats hash equal to main's library's at every step, and two invalid inputs give the same errors. Tests: tf_compile_test.cc drives the runner with fake compiles through the deadline, late results kept out of the cache, a small compile passing a running large one, shared jobs, slot waits, ownership past close, failures and stack depth. It runs in zig build test and test-grammar-compile-host. A lanes test shows one caller's slow compile no longer holds another's. A server test checks the 400 for every format. Through a real libtfgrammar, the 8,000-property schema is refused at 2 s while a small schema compiles, a retry joins the running compile, and once that returns the grammar comes from the cache. Fixes #3209
ashhart
force-pushed
the
lead/grammar-compile-bound
branch
from
November 18, 2026 20:43
5bc8588 to
bdd4af8
Compare
joshuaswarren
pushed a commit
to joshuaswarren/TensorFold
that referenced
this pull request
Nov 19, 2026
This was referenced Nov 19, 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.
Main compiles every grammar under the server-wide grammar lock with no time bound. xgrammar's compile time grows faster than the square of an object's optional properties, so one such schema holds every other grammar request behind it, cached ones included. An object of 8,000 optional properties, 207 KB, takes minutes.
What changes
response_format: the grammar cannot be enforced: grammar compilation exceeds the 2000 ms time limit. This covers JSON objects and schemas, regexes, choices, EBNF and the structural tags of tool calls.tfg_compiling, a slot count for tests, and an older libtfgrammar still compiles without the limit.What happens to an abandoned compile: xgrammar has no point at which a compile can be stopped. An abandoned compile runs to the end on its thread and keeps its slot until it returns, so no more than 4 run at once. While it runs, the same input joins it instead of compiling again. Its grammar goes into xgrammar's cache, so the same request sent after it finishes is served. The new cache never stores a failure.
Why grammars within the limit are unchanged: the compiler object, calls and options are main's; only the thread they run on changed. Mask hashes after every step of an accepted reply, over 14 grammars in all six formats, match main's library exactly; first masks over a 248K-token vocabulary match too, and invalid inputs give the same errors.
test-grammar-toolsandtest-grammar-budgetpass unchanged.Tests
test-grammar-compile-host, also run byzig build test, drives the compile runner with fake compiles: the deadline, late results kept out of the cache, a small compile passing a running large one, two callers sharing one compile, slot waits, ownership past close, failures, and a compile that needs 4 MiB of stack.test-grammar-tools -Dxgrammar=DIRrefuses the 8,000-property schema at the 2,000 ms limit while a small schema compiles beside it; a retry joins the running compile, and once that compile returns the grammar is served from the cache (fails on main).The Windows compile-thread path builds and links here; the zig-windows CI job is its first run.
Fixes #3209