Skip to content

perf(parser): memoize createTrie to avoid rebuilding tries per Tokenizer - #911

Merged
harttle merged 1 commit into
harttle:masterfrom
amedve:createTrie-perf
Jun 22, 2026
Merged

harttle merged 1 commit into
harttle:masterfrom
amedve:createTrie-perf

Conversation

@amedve

@amedve amedve commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

What

Memoize createTrie (src/util/operator-trie.ts) with a module-level WeakMap keyed on the input object, so the prefix-tries are built once per input and reused across Tokenizer instances.

Why

The Tokenizer constructor calls createTrie(operators) and createTrie(literalValues) on every instantiation, and LiquidJS constructs a fresh Tokenizer per 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

  • The inputs are stable references: literalValues is a module-level constant, and options.operators is set once per Liquid instance.
  • The trie is only ever read afterward (via matchTrie), never mutated, so caching by reference is behavior-preserving.
  • WeakMap (not Map) is used so that short-lived, per-instance operator objects (e.g. the {} literal passed in Hash), and their tries can be garbage collected. No unbounded growth.
  • A comment documents that the returned trie must be treated as read-only, since createTrie is re-exported from src/index.ts (public API).

Benchmarks

Built with npm run build:cjs and compared against [email protected] from unpkg, on benchmark/templates/todolist.liquid (arm64, Node, 5s windows):

Path latest 10.27.0 local (this PR) change
parse (engine.parse) 18,807 parse/s (53.2 µs) 37,194 parse/s (26.9 µs) +97.8% (~2x)
render (npm run perf:diff) 24,996 ops/s 25,008 ops/s +0.047% (noise)

The official perf:diff harness parses the template once outside the timed loop and only times render, 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

  • Added a #createTrie() block in src/parser/tokenizer.spec.ts asserting that the same input returns the same trie reference and distinct inputs return distinct tries.

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.
@harttle
harttle merged commit 3a0d80d into harttle:master Jun 22, 2026
14 checks passed
@harttle

harttle commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Relates to #866, the change seems acceptable. Merging and planning for thorough fix on v11.

@harttle

harttle commented Jun 22, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @amedve for code

@allcontributors

Copy link
Copy Markdown
Contributor

@harttle

@amedve already contributed before to code

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))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 10.27.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants