Skip to content

Native {% blocktranslate %}, {% static %} and humanize naturaltime (0.4.0) - #8

Merged
MasonLyons merged 2 commits into
mainfrom
perf/native-blocktrans-static-naturaltime
Oct 18, 2026
Merged

MasonLyons merged 2 commits into
mainfrom
perf/native-blocktrans-static-naturaltime

Conversation

@MasonLyons

Copy link
Copy Markdown
Owner

On a realistic backend+request page (benchmarks/realworld.py), the three biggest Python-bound pieces left were {% blocktranslate %}, {% static %} and humanize's naturaltime. This PR makes all three native and bumps the version to 0.4.0.

Changes

  • {% blocktranslate %} / {% blocktrans %} compile to a native node (src/tags/i18n.rs). It supports count, context, with, trimmed, asvar, plural forms, and the stock fallbacks for missing keys and bad formats. A shared filters::Translator handles gettext, pgettext, ngettext and npgettext.
  • {% static %} renders natively when StaticNode.handle_simple is the stock method. With the exact stock StaticFilesStorage, storage.url results are memoized per (base_url, path). setting_changed drops base_url, which changes the key. Other storages (Manifest, custom) still call their own url().
  • humanize naturaltime is native. Its msgids are read from the stock lazy proxies instead of being hardcoded, because they contain U+00A0.
  • README: these three tags and filters moved from the "bridged" list to the native list, and the realworld figure is updated.

Performance (pinned to an idle core, two runs each)

main this PR
realworld page 2.6x (2.9 ms) 3.5x (2.2 ms)
blocktranslate 1.1x 6–7x
static 1.3x 10x
naturaltime 1.2x 4.2x

bench.py geomean: render 14.53x → 14.41x, compile 12.17x → 11.83x. Most rows are within noise. Repeated runs show two small but real render slowdowns: inclusion_tag (about 4.45x → 4.2x) and csrf_token_loop (about 12.1x → 11.5x). Neither path touches the new code. The new parse hooks reject other tags with a cached pointer-identity check, and the new payloads are boxed, so Node is the same size. The most likely cause is code layout from the larger dispatch match. Worth a follow-up, but it doesn't block this PR.

Testing

  • pytest: 1252 passed, in default, realpage-first and reversed file orders. This adds parity tests for blocktranslate, static (with and without staticfiles, three STATIC_URLs) and naturaltime (humanize_tests' fixed clock, naive, aware and odd tzinfo).
  • cargo test --release passes.
  • Django template suite: 100% on stable/6.1.x (5933 Rust renders) and stable/5.2.x (5530).
  • Django i18n, humanize_tests, timezones, forms_tests, admin_views and staticfiles_tests through the Rust engine on 6.1: 100% (56,607 Rust renders).

After merge: tag v0.4.0 on the merge commit to publish.

🤖 Generated with Claude Code

MasonLyons and others added 2 commits October 17, 2026 23:57
These three were the largest Python-bound pieces left on a realistic
backend+request page (benchmarks/realworld.py).

- {% blocktranslate %} / {% blocktrans %} compile to a native node
  (src/tags/i18n.rs): count/context/with/trimmed/asvar, plural forms and
  the stock missing-key and bad-format fallbacks. gettext, pgettext,
  ngettext and npgettext go through a shared filters::Translator.
- {% static %} renders natively when handle_simple is stock. With the
  exact StaticFilesStorage, storage.url results are memoized per
  (base_url, path); other storages (Manifest, custom) still call url().
- humanize naturaltime is native. Msgids are read from the stock lazy
  proxies rather than hardcoded (they contain U+00A0).

realworld page: 2.6x -> 3.5x over stock (2.9 ms -> 2.2 ms).
blocktranslate 1.1x -> 6-7x, static 1.3x -> 10x, naturaltime 1.2x -> 4.2x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MasonLyons
MasonLyons merged commit 9436c4d into main Oct 18, 2026
15 checks passed
@MasonLyons
MasonLyons deleted the perf/native-blocktrans-static-naturaltime branch October 18, 2026 00:08
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