-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Optimize DateTime.Equals #59857
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
Optimize DateTime.Equals #59857
Conversation
|
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
|
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsA tiny improvement for bool Test1(DateTime d1, DateTime d2) => d1 == d2;
// cmp against a constant
bool Test2(DateTime d1, DateTime d2) => d1 == new DateTime(637686893321649705);Codegen diff: https://www.diffchecker.com/v4BfSbey Self-contained benchmark: https://gist.github.com/EgorBo/e5642d9c2d362bb53880d3c9c87e5b95
|
| public static bool Equals(DateTime t1, DateTime t2) | ||
| { | ||
| return t1.Ticks == t2.Ticks; | ||
| return t1 == t2; |
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.
Looks like we have similar pattern in TimeOnly class:
| public bool Equals(TimeOnly value) => _ticks == value._ticks; |
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.
TimeOnly's one compares two fields so it's ok. DateTime compared two computed properties
|
What does it do to perf on 32-bit platforms? (I think it is ok to take this even it makes 32-bit slower, but it would be useful to have it as a datapoint.) |
|
Just a guess - masking the top two bits instead of shifting may be significantly faster on 32-bit, and about the same on 64-bit. |
It seems it's still faster on 32bit, here is the codegen diff: https://www.diffchecker.com/vexK8vcb
Looks like it hurts x64 because |
Can you add this to dotnet/performance ? |
Sure, it seems we don't cover these APIs there |
A tiny improvement for
DateTime.Equals:Codegen diff: https://www.diffchecker.com/v4BfSbey
Could be done in JIT but it needs forward-sub feature for that first.
Self-contained benchmark: https://gist.github.com/EgorBo/e5642d9c2d362bb53880d3c9c87e5b95