-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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 a method consuming ReadOnlySpan<byte> to System.HashCode #48702
Comments
Thanks! Might want to add an instance member as well so that hash codes of complex data types can be created. |
Something like this you mean? public struct HashCode
{
public void Add(ReadOnlySpan<byte> ros);
} |
Yes. That would be required if we wanted to solve #4761 for complex key types like tuples. For instance, a collision-resistant hash code routine for var hashCode = new HashCode();
hashCode.Add(in tuple.Item1); // call to internal API
hashCode.Add(in tuple.Item2); // call to internal API
return hashCode.ToHashCode(); With the following internal implementation APIs: public struct HashCode
{
internal void Add<T>(in T value) where T : INonPigeonholingHashCode;
}
internal interface INonPigeonholingHashCode
{
void AddHashCodeTo(ref HashCode hashCodeCombiner);
}
public readonly struct Int64 : INonPigeonholingHashCode
{
void INonPigeonholingHashCode.AddHashCodeTo(ref HashCode hashCodeCombiner)
{
// predicated on HashCode.Add(ROS<byte>) existing
hashCodeCombiner.Add(MemoryMarshal.AsBytes(MemoryMarshal.AsReadOnlySpan(ref this, 1));
}
} |
Would it make sense to also add a generic overload that takes |
Probably wouldn't do generic |
I don't have a strong opinion, but is HashCode h = default;
h.Add(ros);
return h.ToHashCode(); ? Or is the perf of that sufficiently different that it warrants a dedicated implementation? |
If there is a perf difference I imagine it'll be trivial. There's nothing stopping us from adding the one-shot later if people ask for it. |
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsBackground and MotivationOriginally posted by @GrabYourPitchforks in #48410 (comment)
Proposed APInamespace System
{
public struct HashCode
{
public int Add(ReadOnlySpan<byte> ros);
}
} Usage ExamplesUseful in defining equality comparison for buffers, which has a few applications in libraries code. See #48410 for context. RisksNone known. cc @stephentoub
|
This would be very nice to have 😄 Related, we currently have the If this API proposal gets approved I'd be happy to have the implementation in the |
namespace System
{
public struct HashCode
{
public void Add(ReadOnlySpan<byte> value);
}
} |
Background and Motivation
Originally posted by @GrabYourPitchforks in #48410 (comment)
Proposed API
Usage Examples
Useful in defining equality comparison for buffers, which has a few applications in libraries code. See #48410 for context.
Risks
None known.
cc @stephentoub
The text was updated successfully, but these errors were encountered: