Repository navigation
Faster |localtime / |timezone output and native humanize |ordinal - #10
Merged
Merged
Conversation
- 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>
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.
Two filters that an audit of the remaining Python paths found running at about 1x stock Django.
|localtime/|utc/|timezoneThese filters return
django.templatetags.tz.datetimeobject, a baredatetimesubclass whose only addition is aconvert_to_local_timeinstance attribute. The native datetime paths (template_localtime,dateformat.format,render_value_in_context) only accepted an exactdatetime, so every filtered value fell back to Python'sdateformat. They now accept the stockdatetimeobjectas well. It is found throughsys.modules, so nothing gets imported, and it is cached only if its class body is empty.convert_to_local_timeis honoured exactly like stock. Further subclasses still take the Python path.humanize
|ordinalStock 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 exactints, and patchedpgettext/mark_safemodule globals, call the Python function.Numbers
20-row loop, pinned core:
{{ now|localtime }}{{ now|timezone:'Europe/Paris' }}{{ n|ordinal }}Tests
New parity cases in
tests/test_realpage.py:datetimeobjects with the flag unset, True or falsy. Each runs under{% localtime off %}, through|date/|timewith tz-specific specifiers, withUSE_TZon and off, and in three locales.datetimeobject.ordinalover edge values (11–13, 111, i64 bounds, bigints, bools, strings, floats) in seven locales and with i18n off.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