-
Notifications
You must be signed in to change notification settings - Fork 543
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
Refactor unix::Cache
#1529
base: main
Are you sure you want to change the base?
Refactor unix::Cache
#1529
Conversation
Thanks for doing the legwork on this! Will try to take a look soon. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1529 +/- ##
==========================================
- Coverage 91.79% 91.52% -0.27%
==========================================
Files 37 37
Lines 18175 18153 -22
==========================================
- Hits 16683 16614 -69
- Misses 1492 1539 +47 ☔ View full report in Codecov by Sentry. |
7bb8390
to
541c9ba
Compare
Of course, and thank you. Removed one commit that in the end didn't do much. As a note: the reason to make everything methods on the |
541c9ba
to
f99aa51
Compare
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.
I've tried to review this, but I've found it challenging.
Most of the changes don't look like improvements to me -- they mostly add more code, and seem to make it all more complex. Tell me why/how I'm wrong?
if old_mtime != mtime => | ||
{ | ||
true | ||
(Source::LocalTime, Source::LocalTime) => { |
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.
Why is this better? It seems less precise to me.
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.
Hmm, maybe I should keep an mtime around. It is not guaranteed to be the real modification time. We should either use ctime or keep track of the mtime.
Comparing ctime to last_checked
is simpler though.
src/offset/local/unix.rs
Outdated
self.zone = current_zone(env_ref); | ||
} | ||
if self.needs_update() { | ||
let env_tz = env::var("TZ").ok(); |
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.
The duplication between here and needs_update()
doesn't seem great...
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.
Thought the same! But it seemed premature optimization to remove this one duplicate read of an environment variable. We only do so if the variable has changed, which should be rare.
We can keep the variable around of course. It will have to be returned or passed to a couple of methods. More than what is visible in this PR as the final commit in #1457 adds more methods.
src/offset/local/unix.rs
Outdated
enum Source { | ||
Environment { hash: u64 }, | ||
LocalTime, | ||
Uninitialized, |
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.
Can we just represent this as Option<Source>
instead? Seems more idiomatic to me.
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.
I'll update it to use an Option
.
src/offset/local/unix.rs
Outdated
thread_local! { | ||
static TZ_INFO: RefCell<Option<Cache>> = Default::default(); | ||
static TZ_INFO: RefCell<Cache> = const { RefCell::new( | ||
Cache { |
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.
Why? This doesn't seem like an improvement.
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.
It is a preparation for the next commit: splitting up the offset
function.
The old code did all in the initialization in offset()
using Cache::default
on first use within a thread.
The offset_from_utc_datetime
and offset_from_local_datetime
both had to go through the offset
function even though they had little in common (and preferably not the same return type), so they could use the same get_or_insert_with
.
The goal of this commit is to move the concern of initialization from the caller the Cache
type.
When looking at it commit by commit it looks that way. Once you get to "Move parsing of And this mostly prepares for the final commit in #1457. Could you give the end result a global look? |
I'm really sorry but even after looking at all of the changes in #1457 today it doesn't seem more clear -- and it adds 80 lines of code, is that all for the new functionality? That seems to remove some tests so, if anything, seems to be undercounting library code. It seems to add lots of small functions which IMO just make it harder to follow the flow. |
I put in the effort to slice the first commit of #1457 into 17 commits that each are readable as a refactor.
The goal is to move all logic for determining and parsing the time zone with fallbacks in one layer.
This will make it possible to properly hook up errors, and to make the cache invalidation work beyond the most basic cases.
But those improvements are not included in this PR.
The last commit, adding support for the
TZ_DIR
variable, is the only noticeable change in functionality.