Skip to content

Faster |localtime / |timezone output and native humanize |ordinal - #10

Merged
MasonLyons merged 1 commit into
mainfrom
perf/datetimeobject-ordinal
Oct 18, 2026
Merged

MasonLyons merged 1 commit into
mainfrom
perf/datetimeobject-ordinal

Conversation

@MasonLyons

Copy link
Copy Markdown
Owner

Two filters that an audit of the remaining Python paths found running at about 1x stock Django.

|localtime / |utc / |timezone

These filters return django.templatetags.tz.datetimeobject, a bare datetime subclass whose only addition is a convert_to_local_time instance attribute. The native datetime paths (template_localtime, dateformat.format, render_value_in_context) only accepted an exact datetime, so every filtered value fell back to Python's dateformat. They now accept the stock datetimeobject as well. It is found through sys.modules, so nothing gets imported, and it is cached only if its class body is empty. convert_to_local_time is honoured exactly like stock. Further subclasses still take the Python path.

humanize |ordinal

Stock translates all ten suffix templates on every call, which is 9 gettext calls per value. The native port translates only the one template it uses (through Translator::pgettext), and formats it natively when the translation is a plain {} template. Inputs that aren't exact ints, and patched pgettext/mark_safe module globals, call the Python function.

Numbers

20-row loop, pinned core:

construct before after
{{ now|localtime }} 1.05x 5.2x
{{ now|timezone:'Europe/Paris' }} 1.07x 9.8x
{{ n|ordinal }} 1.07x 24x

Tests

New parity cases in tests/test_realpage.py:

  • tz filters with aware, naive, date, str and None values, and datetimeobjects with the flag unset, True or falsy. Each runs under {% localtime off %}, through |date/|time with tz-specific specifiers, with USE_TZ on and off, and in three locales.
  • A subclass of datetimeobject.
  • ordinal over edge values (11–13, 111, i64 bounds, bigints, bools, strings, floats) in seven locales and with i18n off.
  • Patched globals.

Results: pytest 1338 passed (6.1) and 1326 passed (5.2). The Django template suite passes on 6.1 (100%) and on 5.2 (only the curated root-only failure).

🤖 Generated with Claude Code

- The tz filters (`localtime`, `utc`, `timezone`) return
  `django.templatetags.tz.datetimeobject`, a bare datetime subclass whose
  only addition is the `convert_to_local_time` instance attribute. The
  native datetime paths (`template_localtime`, `dateformat.format`,
  `render_value_in_context`) only accepted an exact datetime, so every
  filtered value fell back to Python's dateformat. They now accept the
  stock `datetimeobject` too, found via `sys.modules` and used only while
  its class body is still empty. `convert_to_local_time` is honoured
  like stock.
- `ordinal` translated all ten suffix templates on every call (9 gettext
  calls per value). The native port translates only the template it
  needs, through `Translator::pgettext`, and formats natively when the
  translation is a plain `{}` template. Non-int inputs and patched
  `pgettext`/`mark_safe` globals take the Python function.

gaps microbench (20-row loop): |localtime 1.05x -> 5.2x, |timezone
1.07x -> 9.8x, |ordinal 1.07x -> 24x faster than stock.

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