Conversation
Avoid serializing and reparsing an existing Grammar when populating the compiler cache while preserving shared cache keys and concurrent miss deduplication.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves grammar compilation performance by letting the grammar cache compute values per call on cache misses, enabling direct compilation of existing Grammar objects without an extra Grammar -> EBNF text -> Grammar round-trip while preserving the existing cache keying and bounded/unbounded behaviors.
Changes:
- Extend
ThreadSafeLRUCache::Getto accept an optional per-call “compute on miss” callable (while keeping the existing default-computer behavior). - Update
GrammarCompiler::Impl::CompileGrammar(const Grammar&)to compile the passedGrammarobject directly on cache misses, keyed by the canonical text/root tuple so object/string compilation shares cache entries. - Add C++ and Python tests covering per-call miss computation override and shared caching between
Grammarobjects and EBNF strings.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
cpp/support/thread_safe_cache.h |
Adds a Get(key, compute_on_miss) overload and threads it through the internal future computation path. |
cpp/grammar_compiler.cc |
Uses per-call miss computation to compile a Grammar object directly when inserting a cache entry. |
tests/cpp/test_thread_safe_cache.cc |
Adds coverage for the per-call compute-on-miss override behavior in both unlimited and bounded modes. |
tests/python/test_grammar_compiler.py |
Adds coverage ensuring compile_grammar(Grammar) and compile_grammar(str(grammar)) share cache results and match no-cache output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
15 tasks
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.
改动
Grammar语法对象,避免“语法对象 -> 扩展巴科斯范式文本 -> 语法对象”的多余往返。扩展巴科斯范式(Extended Backus-Naur Form,EBNF)是描述语法规则的文本表示。性能
与主分支提交
c5717178比较。输入是包含 400 个函数的结构化标签语法,使用 Qwen3-4B-Instruct-2507 语言模型的词表数据和 8 个固定处理器核心。两组交替运行,合计取 20 个样本。Grammar对象且缓存未命中:中位数从 453.99 毫秒降到 320.66 毫秒,降低 29.4%,快 1.42 倍。另外进行 10 轮 XGrammar 与 LLGuidance 1.7.6 对照。XGrammar 是本仓库的语法约束生成库,LLGuidance 是另一个语法约束生成实现。改动后,已启动环境下的语法编译分别为 399.4 毫秒和 118.3 毫秒;单次词元允许集合生成的中位数分别为 2.6 微秒和 609.0 微秒。
验证