[Python-Dev] Adding a tzidx cache to datetime (original) (raw)
Paul Ganssle paul at ganssle.io
Thu May 9 20:58:58 EDT 2019
- Previous message (by thread): [Python-Dev] Adding a tzidx cache to datetime
- Next message (by thread): [Python-Dev] Adding a tzidx cache to datetime
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
This is only "only" for dateutil in the sense that no one other than dateutil implements tzinfo with the interface provided. If dateutil were /not/ already implemented with a list of offsets and their indexes, I would still propose this, and just re-write dateutil to take advantage of it. From a cursory glance at pendulum, it seems that they could take advantage of it as well (though they use their own datetime subclass, so they have always had the ability to add this).
What do you think of adding a private "cache" attribute which would be an arbitrary Python object? (None by default)
We cannot use a private attribute (other than to do the actual storage, since the thing that gets stored is not directly accessible anyway and is instead mediated by a layer that manages the cache) because this is a feature explicitly being added for use by tzinfo, /not/ by datetime. If it's private then it's not safe for implementations of tzinfo to actually use it, which defeats the purpose.
Regarding the use of an arbitrary Python object: What I'm proposing is that we offer a bit of the "free" storage space in the alignment bits to tzinfo objects to use as a cache. In /most/ cases this will be very useful to someone implementing a tzinfo, because there are really only so many ways to accomplish this task, and most time zones are expressible as a very short list of offset/name/dst combinations, plus some rule for which applies when, which is why a small integer cache is sufficient and more or less universal (i.e. not specific to dateutil's implementation).
I will also note that in my design, it is still possible for tzinfo
to
return something other than [0, 254], it's just that that information
will not be cached, so it won't get the benefit of any optimization, but
the same interface / implementation can be used.
In my test with gcc, adding an additional PyObject* to the end of the
PyDateTime_DateTime struct increased the size of the datetime.datetime
object from 64 to 72 bytes, whereas adding an unsigned char
after the
fold
leaves it unchanged. Given that the expansion to arbitrary Python
objects is speculative and doesn't have any particular use case, I would
prefer to leave the feature as is, and reconsider the possibility of
storing arbitrary Python objects on the datetime if there's some
compelling reason to do so (it would be a backwards-compatible change at
that point anyway).
On 5/9/19 8:14 PM, Victor Stinner wrote:
Hi Paul,
The change is basically an optimization. I'm uncomfortable to design it "only" for dateutil. What if tomorrow someone has to store an arbitrary Python object, rather than just an integer (in range [0; 254]), into a datetime for a different optimization? Moreover, I dislike adding a public method for an internal cache. Right now, it is not possible to create a weak reference to a datetime. If we make it possible, it would be possible to have an external cache implemented with weakref.WeakSet to clear old entries when a datetime object is detroyed. What do you think of adding a private "cache" attribute which would be an arbitrary Python object? (None by default) Victor Le mar. 7 mai 2019 à 21:46, Paul Ganssle <paul at ganssle.io> a écrit : Greetings all,
I have one last feature request that I'd like added to datetime for Python 3.8, and this one I think could use some more discussion, the addition of a "time zone index cache" to the datetime object. The rationale is laid out in detail in bpo-35723. The general problem is that currently, every invocation of utcoffset, tzname and dst needs to do full, independent calculations of the time zone offsets, even for time zones where the mapping is guaranteed to be stable because datetimes are immutable. I have a proof of concept implementation: PR #11529. I'm envisioning that the
datetime
class will add a privatetzidx
single-byte member (it seems that this does not increase the size of the datetime object, because it's just using an unused alignment byte).datetime
will also add atzidx()
method, which will returntzidx
if it's been set and otherwise it will callself.tzinfo.tzidx()
. Ifself.tzinfo.tzidx()
returns a number between 0 and 254 (inclusive), it setstzidx
to this value. tzidx() then returns whatever self.tzinfo.tzidx() returned. The value of this is that as far as I can tell, nearly all non-trivial tzinfo implementations construct a list of possible offsets, and implement utcoffset(), tzname() and dst() by calculating an index into that list and returning it. There are almost always less than 255 distinct offsets. By adding this cache on the datetime, we're using a small amount of currently-unused memory to prevent unnecessary calculations about a given datetime. The feature is entirely opt-in, and has no downsides if it goes unused, and it makes it possible to write tzinfo implementations that are both lazy and as fast as the "eager calculation" mode that pytz uses (and that causes many problems for pytz's users). I have explored the idea of using an lru cache of some sort on the tzinfo object itself, but there are two problems with this: 1. Calculating the hash of a datetime calls .utcoffset(), which means that it is necessary to, at minimum, do areplace
on the datetime (and constructing a new datetime is a pretty considerable speed hit) 2. It will be a much bigger memory cost, since my current proposal uses approximately zero additional memory (not sure if the alignment stuff is platform-dependent or something, but it doesn't use additional memory on my linux computer). I realize this proposal is somewhat difficult to wrap your head around, so if anyone would like to chat with me about it in person, I'll be at PyCon sprints until Thursday morning. Best, Paul
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/vstinner%40redhat.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20190509/fca75bb4/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: OpenPGP digital signature URL: <http://mail.python.org/pipermail/python-dev/attachments/20190509/fca75bb4/attachment.sig>
- Previous message (by thread): [Python-Dev] Adding a tzidx cache to datetime
- Next message (by thread): [Python-Dev] Adding a tzidx cache to datetime
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]