Repository navigation
Native truncatechars_html / truncatewords_html, filesizeformat and intword - #11
Merged
Merged
Conversation
…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>
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.
Makes four filters that still ran through Python native:
truncatechars_html,truncatewords_html,filesizeformatand humanize'sintword. Before this they rendered only 1.2–1.5× faster than stock.New
bench.pyrowtruncate_html_sizes(all four filters over 50 rows): 17.8× vs stock, 16.6× vs Jinja2. Render geomean is now 15.06×.Changes
*_htmltruncation (src/filters/truncate_html.rs): a port ofTruncateCharsHTMLParser/TruncateWordsHTMLParserthat reproduces the parser's callbacks. It covers simple HTML, the grammar the nativestriptagsalready accepts: plain start/end tags with ordinary attributes, plus common named and numeric entity references. This includes the quirks: the whole-inputprocessed_chars == lengthearly 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/SafeStringinputs run the stockTruncator.filesizeformat(exact ints, |n| < 2**53):round(n / 2**k, 1)is computed with integer arithmetic (half-even on the exact value, as Python'srounddoes), then formatted by the nativefloatformatnumber formatter.intword(exact ints, |n| < 2**53): the same float division andround_away_from_oneas stock, andfloatformat(new_value, 1)through the native port. Patchedngettext/defaultfiltersglobals take the Python function.Translator::cached: memoizesgettext/ngettextresults 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.truncatecharsellipsis cache now notices a patcheddjango.utils.text.pgettextand falls back.Verification
USE_THOUSAND_SEPARATOR: 0 mismatches on Django 6.1 and 5.2.Truncatoroutput 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 (theHTMLParsersecurity changes), but none of those inputs reach the native path.tests/test_realpage.py: both truncators over edge-case HTML, args and|safe; non-str inputs; patchedpgettext;filesizeformat/intwordvalues incl. bool, str, float, Decimal, ≥ 2**53 and negatives across locales; i18n off; patchedngettext.cargo test --releasepasses. The clippy warning count is unchanged.humanize_tests,i18nandutils_tests.test_textthrough the Rust engine pass on both.README benchmark tables were regenerated (core 41, two runs agreed within noise).
🤖 Generated with Claude Code