-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add special Comparer/EqualityComparer for Nullable<Enum> #68077
Add special Comparer/EqualityComparer for Nullable<Enum> #68077
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. |
Would it be better to instead figure out how to optimize the boxing from the constrained calls to Enum.Equals? |
} | ||
return -1; | ||
} | ||
} | ||
|
||
// Instantiated internally by VM | ||
internal sealed class NullableEnumEqualityComparer<T> : EqualityComparer<T?> where T : struct, Enum |
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 needs tests and extra code to ensure that the binary serialization continues to work.
This also needs to be handled in src\coreclr\tools\Common\TypeSystem\IL\Stubs\ComparerIntrinsics.cs |
Do we want to add it for Mono too? We generally try to add optimizations like these for all runtimes. |
3f14cf4
to
53cbb24
Compare
How is this fixing the problem with boxing in EqualityComparer from the linker issue? |
@jkotas it's called by the ObjectEqualityComparer. The current problem that
ends up as so while this PR indeed works it's quite verbose and doesn't handle CompareTo so I think it's better to rely on a custom equalitycomparer for now |
It should end up as
runtime/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/EqualityComparer.cs Line 107 in 57bfe47
I do not see anything calling |
it does not currently because it doesn't pass this check runtime/src/coreclr/vm/jitinterface.cpp Lines 8822 to 8827 in 7bfc61b
|
Ok, make sense. |
53cbb24
to
dcd2e3a
Compare
I am wondering whether we can just fix the existing |
da6a93f
to
b6f32f3
Compare
Great idea! Let's see how it goes... |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsFixes #67842 Example: bool Test(DayOfWeek? d1, DayOfWeek? d2) => EqualityComparer<DayOfWeek?>.Default.Equals(d1, d2); Codegen diff: https://www.diffchecker.com/7qPt1KtF Same codegen for e.g.: record struct Test(DayOfWeek? Day);
bool Test(Test t1, Test t2) => t1.Equals(t2);
|
…lable-enum-equality-comparer
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.
Could you please check asm diffs for the existing NullableEqualityComparer uses to make sure there is no significant code quality regression?
Otherwise looks good.
@jkotas Thanks! Sorry for the delay between changes - I was on a month-long "vacation". The codegen diff is more or less the same:
not relevant anymore- it used to rely on fact that a round-trip binary serialization should produce the same or assignable object but it doesn't work now.e.g. EqualityComparer<T> orig = EqualityComparer<T>.Default;
bf.Serialize(s, orig);
s.Position = 0;
object result = bf.Deserialize(s);
Assert.IsAssignableFrom<EqualityComparer<T>>(result); For |
oh, wait a minute that might be a bug actually - let me check |
5be10dc
to
481e782
Compare
Right, it was my bug, fixed with 481e782 |
Failure is unrelated (#68511) |
Fixes #67842
Example:
Codegen diff: https://www.diffchecker.com/7qPt1KtF
It doesn't box (allocate) any more. The final codegen is still a bit suboptimal but it's a different problem (
Nullable<>
is not promoted)Same codegen for e.g.: