-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use threads to update default time zone cache asynchronously #97
use threads to update default time zone cache asynchronously #97
Conversation
fn default_time_zone_benchmark(c: &mut Criterion) { | ||
c.bench_function("Get default TimeZone::system()", |b| { | ||
b.iter(|| { | ||
TimeZone::system(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this PR, this benchmark is improved from 61.665 ns
to 25.429 ns
on my Mac.
I don't think this is the way to go. We shouldn't be starting threads inside a low level library like this. Or at least, I would want a strong ecosystem precedent for it. |
I also think it's not suitable, so only submit a draft PR to check with you, thanks for your quick feedbacck. Do you think An alternative solution is using SystemTIme to check the TTL instead of Instant. Because Of course, if user calls Also, I see the TTL is 5 miuntes by default, so the time error of |
I think this is kinda missing the point of using monotonic time instead of system time on its own. It's not really about "time error." The system time can change arbitrarily and at any time. It's not that system time has some kind of error built into it necessarily. It's just that it's configurable. So if the code relies on time always increasing (or rather, being non-decreasing), then monotonic time is very useful to that end. With that said, I do actually think this is an interesting idea. I don't think we can just get rid of the monotonic time check completely, but we might be able to manage something here because we are dealing with caching. In particular, it's always okay to invalidate the cache. The goal is to just do it infrequently. But if there's an "odd" case where we can't know if the cache is fresh or not, we can fall back to monotonic time.
I think this is not fully bullet proof. In particular, if the I think that should actually do it? I don't immediately see a way for this to go wrong. (Famous last words.) |
Thanks @BurntSushi for the detailed and valuable feedback!
Overall LGTM.
Recording a count of using I have a question: if we find that |
I'm not sure. So pick whichever is simplest to implement. Probably just continue using |
I'm going to close this out because I think this particular avenue for optimization is not the right path to go down. But if you want to try the other idea I put forward, it's probably best to do that in a separate PR. I may also try it myself at some point too. Thanks for the effort! |
close #96
Background
TimeZone::system()
obtains the default time zone, and it may be called frequently by users.Especially,
Zoned::now
will call it. In some systems,Zoned::now
is frequently used to obtain the current time. For example, log system will call it for each log item.How long does
TimeZone::system()
need? and why?aAfter run the benchmark on my Mac, I found
TimeZone::system()
needs61.665 ns
, its cost mainly consists of 3 parts:Instant::now
[3] costs 37.3 ns, the Instant is used for checking whether the cache is expired.So the
Instant::now
(part 3) takes most of time.The idea of optimization
Don't call
Instant::now
during callTimeZone::system()
, and we could start a light async thread to update the default time zone cache periodically.Note: if the default time zone is obtained by async thread, we could update it more frequently. For example: update the default TTL from 5 minutes to 20 seconds or less.
[1]
jiff/src/tz/system/mod.rs
Line 119 in c659069
[2]
jiff/src/tz/system/mod.rs
Line 104 in c659069
[3]
jiff/src/tz/system/mod.rs
Line 106 in c659069
This PR is a draft PR due to it includes some temporary benchmark code or use case.
I will polish this PR if the solution makes sense.