Skip to content

Native truncatechars_html / truncatewords_html, filesizeformat and intword - #11

Merged
MasonLyons merged 1 commit into
mainfrom
perf/truncate-html-filesize-intword
Oct 18, 2026
Merged

MasonLyons merged 1 commit into
mainfrom
perf/truncate-html-filesize-intword

Conversation

@MasonLyons

Copy link
Copy Markdown
Owner

Makes four filters that still ran through Python native: truncatechars_html, truncatewords_html, filesizeformat and humanize's intword. Before this they rendered only 1.2–1.5× faster than stock.

gaps microbench (20-row loop) 0.4.2 dtl now vs stock
truncatechars_html 1105 µs 106 µs 1.2× → 15×
truncatewords_html 726 µs 85 µs 1.2× → 13×
intword 418 µs 48 µs 1.5× → 15×
filesizeformat 337 µs 63 µs 1.4× → 10×

New bench.py row truncate_html_sizes (all four filters over 50 rows): 17.8× vs stock, 16.6× vs Jinja2. Render geomean is now 15.06×.

Changes

  • *_html truncation (src/filters/truncate_html.rs): a port of TruncateCharsHTMLParser / TruncateWordsHTMLParser that reproduces the parser's callbacks. It covers simple HTML, the grammar the native striptags already accepts: plain start/end tags with ordinary attributes, plus common named and numeric entity references. This includes the quirks: the whole-input processed_chars == length early exit, word splitting on (?<=\S)\s+(?=\S), whitespace-only runs counting as a word, unmatched end tags, and <p/> on non-void elements. Comments, raw-text elements, a stray < or &, unknown entities, NULs and non-str/SafeString inputs run the stock Truncator.
  • filesizeformat (exact ints, |n| < 2**53): round(n / 2**k, 1) is computed with integer arithmetic (half-even on the exact value, as Python's round does), then formatted by the native floatformat number formatter.
  • intword (exact ints, |n| < 2**53): the same float division and round_away_from_one as stock, and floatformat(new_value, 1) through the native port. Patched ngettext/defaultfilters globals take the Python function.
  • Translator::cached: memoizes gettext/ngettext results for static msgids, keyed on the translation object itself (held, so its identity can't be reused). A catalog reload creates new objects, so the cache never returns stale strings.
  • The truncatechars ellipsis cache now notices a patched django.utils.text.pgettext and falls back.

Verification

  • Differential fuzz (random HTML from tags, entities, Unicode and whitespace, 11 lengths, 3 languages) and ~20k random ints across 6 locales, with and without USE_THOUSAND_SEPARATOR: 0 mismatches on Django 6.1 and 5.2.
  • Stock Truncator output across Python 3.12, 3.13.0, 3.13.5, 3.13.15, 3.14.0 and 3.14.4 on 240k cases: 3.13.0–3.13.5 differ from the others on some inputs (the HTMLParser security changes), but none of those inputs reach the native path.
  • New parity tests in tests/test_realpage.py: both truncators over edge-case HTML, args and |safe; non-str inputs; patched pgettext; filesizeformat/intword values incl. bool, str, float, Decimal, ≥ 2**53 and negatives across locales; i18n off; patched ngettext.
  • pytest: 1366 pass. cargo test --release passes. The clippy warning count is unchanged.
  • Django template suite at 100% on 6.1 (5937 renders) and 5.2 (5534); humanize_tests, i18n and utils_tests.test_text through the Rust engine pass on both.

README benchmark tables were regenerated (core 41, two runs agreed within noise).

🤖 Generated with Claude Code

…manize intword

- `truncatechars_html` / `truncatewords_html` ran Python's `HTMLParser` on
  every value. They are now a port of `TruncateCharsHTMLParser` /
  `TruncateWordsHTMLParser` for simple HTML (the grammar the native
  `striptags` accepts: plain tags with ordinary attributes and common
  entity references). On that grammar Python 3.12, 3.13 (old and new
  patch releases) and 3.14 produce identical output. Comments, raw-text
  elements, a stray `<` or `&`, unknown entities and non-`str` inputs run
  the stock `Truncator`.
- `filesizeformat` (exact ints below 2**53) rounds the unit value with
  integer arithmetic and formats it through the native `floatformat`
  number formatter.
- humanize `intword` (exact ints below 2**53) uses the same float
  division and `round_away_from_one` as stock, plus the native
  `floatformat`. Patched `ngettext`/`defaultfilters` globals take Python.
- `Translator::cached` memoizes `gettext`/`ngettext` for static msgids per
  translation object. A catalog reload creates new objects, so the cache
  can't serve stale strings.
- The truncation ellipsis cache now falls back when
  `django.utils.text.pgettext` is patched (this also covers
  `truncatechars`).

gaps microbench (20-row loop, vs stock): truncatechars_html 1.2x -> 15x,
truncatewords_html 1.2x -> 13x, intword 1.5x -> 15x, filesizeformat
1.4x -> 10x. New bench.py row truncate_html_sizes: 17.8x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MasonLyons
MasonLyons merged commit c25dbb7 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