Skip to content

HashCode.Combine perf is worse than HashHelpers.Combine #13377

@stephentoub

Description

@stephentoub

We made the public HashCode.Combine method so that developers would use it any time combining is necessary in their own hash code computations. But we use it internally in very few of our own GetHashCode methods, instead preferring to use either the internal HashHelpers.Combine:
https://github.com/dotnet/coreclr/blob/456afea9fbe721e57986a21eb3b4bb1c9c7e4c56/src/System.Private.CoreLib/shared/System/Numerics/Hashing/HashHelpers.cs#L11-L17
or using a custom combine that's generally something as simple as an xor. HashCode.Combine is 3-4x slower than HashHelpers.Combine in today use, e.g. if used in ValueTuple.GetHashCode.

We should either determine that the extra benefits of HashCode.Combine are "worth it" and use it everywhere instead of HashHelpers.Combine, or we should fix HashCode.Combine's perf (likely at the expense of those "benefits") so that it becomes usable. At the moment if we're not comfortable using it ourselves, it's unclear how we could promote it for others to use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions