efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Fix for #34117, Improve Hash Code Distribution for RelationalCommandCache Keys

Open ADNewsom09 opened this issue 7 months ago • 0 comments

This PR addresses a performance issue in the RelationalCommandCache class where the CommandCacheKey struct uses a hard-coded GetHashCode implementation that returns 0. This implementation leads to all instances being placed in the same bucket within the MemoryCache's internal HashSet, causing linear search times and negating the performance benefits of the hash-based collection.

See Issue https://github.com/dotnet/efcore/issues/34117

Changes

  • Modified the GetHashCode implementation in the CommandCacheKey struct to use RuntimeHelpers.GetHashCode(_queryExpression).

  • Ensured that the new implementation maintains the contract with the overridden Equals method, which first checks for reference equality.

ADNewsom09 avatar Jun 28 '24 21:06 ADNewsom09