-
Notifications
You must be signed in to change notification settings - Fork 5k
Extend RuntimeHelpers.IsBitwiseEquatable to more types #75640
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
Conversation
Ugh, fun:
|
Changing the contract on |
Today, RuntimeHelpers.IsBitwiseEquatable is hardcoded to a fixed list of types. This means that almost all of the vectorization we've done with arrays and spans is limited to just those types; developers can themselves use MemoryMarshal.Cast to convert spans of other types to spans of supported one, but it doesn't naturally happen. This extends IsBitwiseEquatable a bit more. We already have a CanCompareBitsOrUseFastGetHashCode helper used by ValueType.Equals to determine whether structs that don't override Equals can be compared with the equivalent of memcmp. This extends that same helper to be used by IsBitwiseEquatable. However, IsBitwiseEquatable also needs to rule out types that implement `IEquatable<T>` (the existing helper doesn't because it's about the implementation of the object.Equals override where the interface doesn't come into play). The upside of this is APIs like Array.IndexOf will now automatically vectorize with more types. The main downside is that types which provide their own equality implementation still don't benefit, which in turn means adding an `IEquality<T>` implementation could in the future be a deoptimization (we should consider some kind of attribute or marker interface a type can use to say "I promise my equality implementation is the same as a bitwise comparison"). We also currently constrain most of our MemoryExtensions methods to types that implement `IEquatable<T>`, so there are only a handful of public methods today that benefit from this.
be3fa02
to
1c908f7
Compare
src/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs
Outdated
Show resolved
Hide resolved
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.
LGTM modulo comments
fc307ac
to
43b1f96
Compare
/azp list |
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
src/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs
Outdated
Show resolved
Hide resolved
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
11b2b9c
to
387f984
Compare
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
src/coreclr/tools/Common/TypeSystem/IL/Stubs/ComparerIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/coreclr/tools/Common/TypeSystem/IL/Stubs/ComparerIntrinsics.cs
Outdated
Show resolved
Hide resolved
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
Today, RuntimeHelpers.IsBitwiseEquatable is hardcoded to a fixed list of types. This means that almost all of the vectorization we've done with arrays and spans is limited to just those types; developers can themselves use MemoryMarshal.Cast to convert spans of other types to spans of supported one, but it doesn't naturally happen.
This extends IsBitwiseEquatable a bit more. We already have a CanCompareBitsOrUseFastGetHashCode helper used by ValueType.Equals to determine whether structs that don't override Equals can be compared with the equivalent of memcmp. This extends that same helper to be used by IsBitwiseEquatable. However, IsBitwiseEquatable also needs to rule out types that implement
IEquatable<T>
(the existing helper doesn't because it's about the implementation of the object.Equals override where the interface doesn't come into play).The upside of this is APIs like Array.IndexOf will now automatically vectorize with more types (and avoid all the boxing overhead and the like with object.Equals). The main downside is that types which provide their own equality implementation still don't benefit, which in turn means adding an
IEquality<T>
implementation could in the future be a deoptimization (we should consider some kind of attribute or marker interface a type can use to say "I promise my equality implementation is the same as a bitwise comparison"); plus, anyone who realizes they have a perf issue will have already added the interface implementation. We also currently constrain most of our MemoryExtensions methods to types that implementIEquatable<T>
, so there are only a handful of public methods today that benefit from this.