Repository navigation
Reuse one Jinjava engine per renderer instead of building one per render - #145
Merged
Merged
Conversation
renderLiteralTemplate built a new Jinjava on every call, and building one registers its whole library of filters, functions and tags. On a short template that construction cost more than the render: 74 us a render before, 47 us after. A realm documents source rendering three templates per record spent 7-9% of a 66,000-record ingest building engines. A Jinjava is safe to share between threads (each render gets its own interpreter). The engine is cached against the two settings it is built from, so a change to the mutable JinjaProperties after construction still takes effect on the next render, as it did when each render read them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why
JinjavaTemplateRenderer.renderLiteralTemplatebuilt a newJinjavaon every call. Building one registers the whole library of filters, functions and tags, and on a short template that costs more than the render itself.Found by profiling an Embabel appliance indexing 12 repositories (66,362 declarations): a realm documents source renders three literal templates per record, and
SimpleLibrary.registershowed up among the top self-time frames. Engine construction was 7–9% of the ingest's CPU.The change
Jinjavais safe to share between threads, because each render gets its own interpreter.JinjaPropertieshas mutablevarfields, so the engine is cached against the two settings it is built from (failOnUnknownTokens,nestedInterpretationEnabled) and rebuilt if either changes. A change after construction therefore still takes effect on the next render, as it did when every render read the properties.Measured
A one-line template, after warm-up: 74 µs → 47 µs per render (−37%). The remaining 47 µs is mostly parsing, since Jinjava re-parses the template on each render; caching parsed templates would be a separate change.
Tests
Three new tests in
JinjavaTemplateRendererTest, all 29 passing:🤖 Generated with Claude Code