[Performance] Optimize Liquid template rendering#7509
[Performance] Optimize Liquid template rendering#7509gonzaloriestra wants to merge 1 commit intomainfrom
Conversation
- Implement a lazy-initialized singleton for the Liquid engine to avoid repeated initialization overhead.
- Add a fast-path for static strings that do not contain Liquid delimiters ({{ or {%).
- Performance improved by ~386x for static strings and ~2.5x for template strings.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Why is this change needed?
Template rendering with Liquid is an expensive operation. Currently,
renderLiquidTemplatecreates a new instance of theLiquidengine on every call, which involves significant overhead (compiling regexes, setting up default tags/filters). Additionally, many strings passed to this function are actually static and don't contain any Liquid tags, yet they still go through the full parsing and rendering pipeline.How to test your changes?
You can verify that Liquid rendering still works correctly by running a command that uses templates, such as creating a new app or extension from a template:
shopify app init --path ./my-app --template nodeDuplicate check
I ran
git log --grep="\[Performance\]" --pretty=format:"%h %s" -n 50andgit log --grep="Liquid" -n 50to ensure this optimization hasn't been attempted. No similar PRs were found.Performance Impact
Benchmark results on 1000 iterations:
The improvement comes from:
Liquidengine instance.{{or{%delimiters.PR created automatically by Jules for task 1714349510327719642 started by @gonzaloriestra