Skip to content

HierarchyId GetHashCode Method returns a limited set of values #3636

Description

@gmw105

Bug description

Having upgraded my project from .NET Framework to .Net 8, and moving to EFCore, I encountered a drastic downturn in performance of client side queries and dictionaries involving HierarchyId.
On investigation I found that the HierarchyId GetHashCode method is providing hash values of only 0-255 (i.e. it is only using the first byte of the 32-bit integer).
This method refers to the GetHashCode method from Microsoft.SqlServer.Types.SqlHierarchyId, so the problem originates there.

The code snippet demonstrates the values for a small test case (all output values are between 0-255).

Your code

using Microsoft.EntityFrameworkCore;

HierarchyId[] hidArray =
    [
        HierarchyId.GetRoot()
        , HierarchyId.Parse(@"/1/1/")
        , HierarchyId.Parse(@"/1/2/3/1/")
        , HierarchyId.Parse(@"/2/19/11/108/43/")
        , HierarchyId.Parse(@"/44/12.5/721.23/1.1/")
        , HierarchyId.Parse(@"/-32/19.1/93485/234/")
        , HierarchyId.Parse(@"/407/")
        , HierarchyId.Parse(@"/999999/1/1/1/")
        , HierarchyId.Parse(@"/12/11/10/9/8/7/6/5/4/3/2/1/")
        , HierarchyId.Parse(@"/-121/921873/564/2343/123/56/")
        , HierarchyId.Parse(@"/99/98/97/")
        , HierarchyId.Parse(@"/13/12/11/10/")
        , HierarchyId.Parse(@"/2/4/6/8/")
        , HierarchyId.Parse(@"/3/5/7/9/")
    ];


foreach(HierarchyId hid in hidArray)
{
    int hc = hid.GetHashCode();
    Console.WriteLine(hc.ToString());
}

Stack traces


Verbose output


EF Core version

9.0.9

Database provider

No response

Target framework

No response

Operating system

No response

IDE

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    External 🔗Issue is in an external componentPerformance 📈Issues that are targeted to performance improvements.

    Type

    Fields

    No fields configured for Bug.

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions