Repository navigation
Faster {% csrf_token %} and inclusion tags - #9
Merged
Merged
Conversation
Both were dominated by Python C-API overhead, not real work:
- Context lookups by name (`Rctx::get`/`get_item`) built a new PyString
for every layer and probed each layer twice (`in`, then `[]`). They now
build one key (or take an interned one) and do a single dict probe per
exact dict/ContextDict layer. {% csrf_token %} uses an interned key.
- Inclusion tags copy `csrf_token` straight into the new context's top
dict (what `Context.__setitem__` does) instead of calling the Python
method, and the native `Context.new` interns its attribute keys and
copies a cached builtins dict.
- `try_render_child` interns its attribute names, builds adapters only
when missing, and skips the render_context push/rebind for rc-free
children, like `Template.render` and the include fast path already do.
bench.py: csrf_token_loop 25.0 -> 15.5 us (12.5x -> 20.1x), inclusion_tag
273 -> 160 us (4.1x -> 6.9x). The suspected 0.4.0 regression in these two
rows did not reproduce on an idle core (0.4.0 was level with 0.3.3).
README benchmark tables regenerated on core 41, now including the five
newer scenarios.
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.
Speeds up
{% csrf_token %}and inclusion tags. In both, most of the time went to Python C-API overhead, not real work.Render geomean is 14.8×.
benchmarks/realworld.pyis unchanged at about 3.5×.Changes
Rctx::get/get_item) used to build a new PyString for every layer and probe each layer twice (in, then[]). They now use one key, interned where the caller has one, and do a single dict probe per exactdict/ContextDictlayer. Other layer types andContextsubclasses keep the generic path.csrf_tokenstraight into the new context's top dict, which is whatContext.__setitem__does, instead of calling the Python method. The nativeContext.newinterns its attribute keys and copies a cached builtins dict.try_render_childinterns its attribute names, builds adapters only when they're missing, and skips the render_context push/rebind for rc-free children.Template.renderand the include fast path already do this.About the suspected 0.4.0 regression
The ~4–5% slowdown in these two rows reported for 0.4.0 doesn't reproduce on an idle core. 0.4.0 measured level with 0.3.3, so it was noise.
Tests
None, missing); the copy is written into the dict the tag function returned, as stock does; an inner{% with %}layer; and aContextsubclass that overridesget.cargo test --releasepasses.README benchmark tables were regenerated (core 41, two runs agreed) and now include the five newer scenarios.
🤖 Generated with Claude Code