-
Notifications
You must be signed in to change notification settings - Fork 13.3k
libstd’s Duration refactor #18690
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
libstd’s Duration refactor #18690
Conversation
This commit changes the internal representation of Duration from pub struct Duraion { secs: i64, nanos: i32 } to pub struct Duration { millis: i64, // Milliseconds nanos: i32, // Nanoseconds, |nanos| < NANOS_PER_MILLI }
Thanks for the PR, @1-more! Would you mind outlining the tradeoffs with this new representation and why the choice is being made? |
That's easy! Show me your data structures, and I won't usually need your code. This is not the case with the old
I think that the new representation is more adequate for our purpose:
And last but not least, while our old representation is meant to be compatible with C like Timespec (see this, the new one is easily convertible to that. |
For reviewers, etc: previous discussion. |
Yeah! According to previous discussion, the best definition of
That is the way .NET and Joda-Time go. It gives the best performance. It fits to alignment rules. The full range of values is large enough etc. But it takes nanoseconds away. Nobody knows do we really need them. At the moment, the only place in Rust code that uses The discussion is still in progress. My commit respects Timespec and nanos, but makes the |
/// An absolute amount of time, independent of time zones and calendars with nanosecond precision. | ||
/// A duration can express the positive or negative difference between two instants in time | ||
/// according to a particular clock. | ||
#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Zero, Default, Hash, Rand)] |
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.
This derived Rand
is incorrect: an arbitrary random i32
x
is highly likely to have |x| >= NANOS_PER_MILLI
violating the restriction on nanos
.
} | ||
|
||
/// Returns the total number of whole microseconds in the duration, | ||
/// or `None` on overflow (exceeding 2^63 microseconds in either direction). | ||
/// Returns the total number of microseconds in the duration. |
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.
This can still overflow, so the mention of when None
is returned should be maintained/updated.
Hm, could you explain why the change made here is neither of the suggestions from the previous discussion?
The stdlib/compiler is not a particularly good place to look for uses of specific functions. This repo will not care or need many functions: they are mainly designed for use by others. A global search of github is a slightly better way to measure the prevalence of a function. |
(To be clear, I don't necessarily think this design is bad, it was just a little surprising to me to follow that discussion and then see something else implemented, so a bit of background motivation for the change would be nice.) |
Well, as I've said before: the discussion is still in progress. And at least one file exists that needs As for |
Sure, the discussion is in progress, but this PR seems to be ignoring that discussion entirely. I don't think the choice here is bad, per se, but I would like to see some justification for it, i.e. why we should change (I see you've justified why we shouldn't leave it as-is above, but the latter is still missing: why is
Just do normal comparison: |
There is no big difference between them. It's In fact, I dislike both and wish the third case ( |
I found one more reason why
to
But it might to be a compatibility issue. Or leave it as is, then it'll be inconsistent. |
…string feat: Use string literal contents as a name when extracting into variable
This commit changes the internal representation of Duration from
to