Skip to content

Change Utils.CombineHashCodes() to use .NET Core's HashCode.Combine() method #7134

Closed

Description

@rjmholt

After discussion in #7126 (comment) and reading various posts (1, 2, 3, 4, 5, 6), it seems like a multiplicative hash function might be better implementation than the current XOR hash used in Utils.CombineHashCodes().

Note that changing the algorithm is, by definition, not a breaking change. To quote the MSDN page on Object.GetHashCode():

A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value... Do not serialize hash code values or store them in databases... Do not send hash codes across application domains or processes. In some cases, hash codes may be computed on a per-process or per-application domain basis... Do not test for equality of hash codes to determine whether two objects are equal. (Unequal objects can have identical hash codes.) To test for equality, call the ReferenceEquals or Equals method.

Also, it would be worthwhile implementing an array overload of Utils.CombineHashCodes(), like Utils.CombineHashCodes(params int[] hs) and Utils.CombineHashCodes(int h, params int[] hs).

Activity

  1. added
    WG-Engine
    Issue-Code Cleanup
    on Jun 22, 2018
  2. lzybkr commented on Jun 23, 2018

    @lzybkr
    Contributor

    That code is copied from .Net here - so if you believe you have a better implementation, consider opening an issue with corefx.

    I think the shift avoids the problems of xor transitivity and that shift + xor is faster than multiplication.

    And last thing - an overload taking params is discouraged because it allocates memory. For non-library code, such an overload is never needed because we can always add 1 more overload that takes the exact number of parameters. Library code does not have that luxury, so libraries make the trade-off of providing a number of overloads for the most common uses and the inefficient overload for all other cases.

  3. iSazonov commented on Jun 23, 2018

    @iSazonov
    Collaborator

    We have several alternative implementations of GetHashCode method in the repo.
    I discovered this when I had to implement this method for SymVer type some time ago. I had the idea of opening a case on this issue but I thought that this is a very rare situation. And this question again appeared. I think we need to revise our code and if possible use the standard implementation from CoreFX.

  4. iSazonov commented on Jun 25, 2018

    @iSazonov
    Collaborator

    We have in .Net Core 2.1
    Discussion System.HashCode
    Implementation
    dotnet/corefx#25013
    dotnet/coreclr#14863

  5. lzybkr commented on Jun 25, 2018

    @lzybkr
    Contributor

    Great, so they added a public api - best to just use that and remove the PowerShell copy.

  6. changed the title [-]Change Utils.CombineHashCodes() to not use XOR + add array overload[/-] [+]Change Utils.CombineHashCodes() to use .NET Core's HashCode.Combine() method[/+] on Jun 25, 2018
  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. added
    Resolution-No Activity
    on Nov 16, 2023
  11. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Code CleanupResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions