Repository navigation
perf(parser): memoize createTrie to avoid rebuilding tries per Tokenizer - #911
Merged
Merged
Conversation
The Tokenizer constructor calls createTrie(operators) and createTrie(literalValues) on every instantiation, and liquidjs builds a fresh Tokenizer per output/tag while parsing. On typical templates this rebuilt the same prefix-tries dozens of times and showed up as a large share of parse CPU in profiling. Memoize createTrie with a module-level WeakMap keyed on the input object. The inputs (operators, literalValues) are stable references and the trie is only ever read afterward (via matchTrie), never mutated, so caching by reference is behavior-preserving. WeakMap (not Map) lets short-lived, per-instance operator objects and their tries be garbage collected.
Owner
|
Relates to #866, the change seems acceptable. Merging and planning for thorough fix on v11. |
Owner
|
@all-contributors please add @amedve for code |
Contributor
github-actions Bot
pushed a commit
that referenced
this pull request
Jun 23, 2026
## [10.27.1](v10.27.0...v10.27.1) (2026-06-23) ### Bug Fixes * improve round function; improvement to [#873](#873) ([#901](#901)) ([956b51e](956b51e)) * **security:** charge pop filter allocation to memoryLimit ([#907](#907)) ([8a0c74a](8a0c74a)) * **strip_html:** infinite loop for strip_html ([5c3522f](5c3522f)) ### Performance Improvements * **parser:** memoize createTrie to avoid rebuilding tries per Tokenizer ([#911](#911)) ([3a0d80d](3a0d80d))
|
🎉 This PR is included in version 10.27.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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
Memoize
createTrie(src/util/operator-trie.ts) with a module-levelWeakMapkeyed on the input object, so the prefix-tries are built once per input and reused acrossTokenizerinstances.Why
The
Tokenizerconstructor callscreateTrie(operators)andcreateTrie(literalValues)on every instantiation, and LiquidJS constructs a freshTokenizerper output/tag while parsing a template. On a typical template this rebuilds the identical tries dozens of times per parse, and the rebuild shows up as a significant share of parse CPU in profiling.Why this is safe
literalValuesis a module-level constant, andoptions.operatorsis set once perLiquidinstance.matchTrie), never mutated, so caching by reference is behavior-preserving.WeakMap(notMap) is used so that short-lived, per-instance operator objects (e.g. the{}literal passed inHash), and their tries can be garbage collected. No unbounded growth.createTrieis re-exported fromsrc/index.ts(public API).Benchmarks
Built with
npm run build:cjsand compared against[email protected]from unpkg, onbenchmark/templates/todolist.liquid(arm64, Node, 5s windows):engine.parse)npm run perf:diff)The official
perf:diffharness parses the template once outside the timed loop and only timesrender, so it (correctly) shows no change for a parse-path optimization, i.e. no render regression. The render path is unaffected; the win is in parse/engine.parse, which the table above isolates.Tests
#createTrie()block insrc/parser/tokenizer.spec.tsasserting that the same input returns the same trie reference and distinct inputs return distinct tries.