Skip to content

Faster {% csrf_token %} and inclusion tags - #9

Merged
MasonLyons merged 1 commit into
mainfrom
perf/inclusion-csrf
Oct 18, 2026
Merged

MasonLyons merged 1 commit into
mainfrom
perf/inclusion-csrf

Conversation

@MasonLyons

Copy link
Copy Markdown
Owner

Speeds up {% csrf_token %} and inclusion tags. In both, most of the time went to Python C-API overhead, not real work.

bench.py row before after vs stock
csrf_token_loop 25.0 µs 15.5 µs 12.5× → 20.1×
inclusion_tag 273 µs 160 µs 4.1× → 6.9×

Render geomean is 14.8×. benchmarks/realworld.py is unchanged at about 3.5×.

Changes

  • Context lookups by name (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 exact dict/ContextDict layer. Other layer types and Context subclasses keep the generic path.
  • Inclusion tags copy csrf_token straight into the new context's top dict, which is what Context.__setitem__ does, instead of calling the Python method. 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 they're missing, and skips the render_context push/rebind for rc-free children. Template.render and 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

  • New parity tests: csrf inside an inclusion child (escaped, None, missing); the copy is written into the dict the tag function returned, as stock does; an inner {% with %} layer; and a Context subclass that overrides get.
  • pytest: 1252 + new tests pass. cargo test --release passes.
  • Django template suite at 100% on 6.1 (5937 renders) and 5.2 (5534).
  • admin_views, admin_inlines, admin_changelist, forms_tests, i18n, humanize_tests and timezones through the Rust engine: 2158 tests OK.

README benchmark tables were regenerated (core 41, two runs agreed) and now include the five newer scenarios.

🤖 Generated with Claude Code

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>
@MasonLyons
MasonLyons merged commit 3979a94 into main Oct 18, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant