<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>This is only "only" for dateutil in the sense that no one other
than dateutil implements tzinfo with the interface provided. If
dateutil were <i>not</i> 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).<br>
</p>
<p>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">What do you think of adding a private "_cache" attribute which would
be an arbitrary Python object? (None by default)</pre>
</blockquote>
<br>
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, <i>not</i> by datetime. If it's private then it's
not safe for implementations of tzinfo to actually use it, which
defeats the purpose.<br>
<br>
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 <i>most</i>
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).<br>
<br>
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.<br>
<br>
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).<br>
<br>
</p>
<div class="moz-cite-prefix">On 5/9/19 8:14 PM, Victor Stinner
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CA+3bQGHTziD9zi3W6o_ZrO9BGtxpV6x8WEuDV_8EY-+Q9Lqvag@mail.gmail.com">
<pre class="moz-quote-pre" wrap="">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 <a class="moz-txt-link-rfc2396E" href="mailto:paul@ganssle.io"><paul@ganssle.io></a> a écrit :
</pre>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">
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 private `_tzidx` 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 a `tzidx()` method, which will return `_tzidx` if it's been set and otherwise it will call `self.tzinfo.tzidx()`. If `self.tzinfo.tzidx()` returns a number between 0 and 254 (inclusive), it sets `_tzidx` 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 a `replace` 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
<a class="moz-txt-link-abbreviated" href="mailto:Python-Dev@python.org">Python-Dev@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-dev">https://mail.python.org/mailman/listinfo/python-dev</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/options/python-dev/vstinner%40redhat.com">https://mail.python.org/mailman/options/python-dev/vstinner%40redhat.com</a>
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">
</pre>
</blockquote>
</body>
</html>