-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Make RuntimeHelpers.IsBitwiseEquatable public #46017
Comments
I don't think we need to make Edit: bitwise equatable-agnostic logic would look something like this, which is a slight modification on your proposal: if (comparer is null)
{
ROS<T> span1;
if (first is T[] arr) span1 = arr;
else if (first is List<T> list) span1 = CollectionsMarshal.Whatever(list);
else goto SlowPath;
ROS<T> span2;
if (second is T[] arr) span2 = arr;
else if (second is List<T> list) span2 = CollectionsMarshal.Whatever(list);
else goto SlowPath;
return span1.SequenceEqual(span2);
}
SlowPath:
/* existing logic */ |
@GrabYourPitchforks LINQ's |
Then we should look into removing the constraint on |
Aside - if we do end up making |
Ah I had it in the description but decided to remove. |
As of .NET 6, As for piggybacking on
A more dynamic implementation of Either of the following would be great:
|
I just realized that having such an attribute might actually be far superior to other solutions. Without the attribute, we would have to rely on Rather, an attribute (only permitted on |
Enumerable.SequenceEqual does always just delegate to MemoryExtensions.SequenceEqual now if we extract an array: runtime/src/libraries/System.Linq/src/System/Linq/SequenceEqual.cs Lines 27 to 30 in c3cc9fd
using the unconstrained MemoryExtensions.SequenceEqual overload. Is there anything left to do for this issue or can it be closed? |
Wow, didn't know that, at the point of creating this issue only |
Background and Motivation
This proposal makes RuntimeHelpers.IsBitwiseEquatable API public.
It allows users to optimize various comparisons with super fast memcmp/memset-like APIs when input T is bitwise comparable (e.g.
byte
,int
,some struct without gc fields and holes in its layout)Proposed API
Usage Examples
A good example is to use it in
System.Linq.dll
forSequnceEquals
. I noticed that people still widely use it to compare arrays of primitives, e.g.:(benchmark ^: https://gist.github.com/EgorBo/4e151a32b10d997937fd212ab842450c)
I understand that we promote spans for performance but I believe we can optimize the general LINQ API almost for free in this case.
With
RuntimeHelpers.IsBitwiseEquatable
public we can make the following change to the LINQ'sSequnceEquals
to make it super fast for such bitwise comparableTSource
types:The text was updated successfully, but these errors were encountered: