bpo-35723: Proof of concept for tzidx cache by pganssle · Pull Request #11529 · python/cpython (original) (raw)
When examining the performance characteristics of pytz, I realized that pytz's eager calculation of tzname, offset and DST gives it an implicit cache that makes it much faster for repeated queries to .utcoffset(), .dst() and/or .tzname(), see my blog post "pytz: The Fastest Footgun in the West", though the eager calculation means that it's slower to create an aware datetime that never calculates those functions.
This proof of concept introduces a "set once" cache to the datetime
object that tzinfo
implementations can use to cache offset and name lookups per datetime. A more thorough discussion of the rationale for this change is available on the associated issue, bpo-35723.
I will note that this is currently a WIP, it still needs:
- Proper handling of what happens when this function is called in an infinite recursive loop (currently segfaults)
- Tests for the situation where
tzidx
is not an integer, or outside the interval [0, 254] - Documentation of the API
- News entry