perf: reuse Liquid engines and hoist constant regexes in MJML converter#381
Open
Amahdip wants to merge 1 commit into
Open
perf: reuse Liquid engines and hoist constant regexes in MJML converter#381Amahdip wants to merge 1 commit into
Amahdip wants to merge 1 commit into
Conversation
processLiquidContent created a new SecureLiquidEngine (fresh environment + standard tag registration) for every block on every render, and cleanLiquidTemplate/camelToKebab recompiled constant regexes per call. All three run per-block during email rendering, so a broadcast pays this cost thousands of times. Engines are now reused through a sync.Pool. A pool is used instead of a shared instance because liquid.Environment caches strainer classes in an unsynchronized map during rendering, so a single engine is not safe for concurrent use. Verified with go test -race. BenchmarkProcessLiquidContent: 28424 ns/op -> 10537 ns/op (-63%), 338 allocs/op -> 120 allocs/op BenchmarkProcessLiquidContentParallel: 17504 ns/op -> 2418 ns/op (-86%) BenchmarkCamelToKebab: 624 ns/op -> 231 ns/op (-63%)
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.
Problem
processLiquidContentcreates a newSecureLiquidEngine— a freshliquid.Environmentplus full standard-tag registration — for every block of every render.cleanLiquidTemplateandcamelToKebabalso recompile constant regexes per call. All three run per-block during email rendering, so a broadcast pays this cost thousands of times.Fix
Reuse engines through a
sync.Pooland hoist the regexes to package level.A pool is used instead of a shared instance deliberately: liquidgo's
Environment.CreateStrainerreads and writesstrainerTemplateClassCachewithout a lock during rendering, so a single engine must not be shared across goroutines. Verified withgo test -race ./pkg/notifuse_mjml/.Results