Skip to content
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

Improve performance for RelationalCommandCache by adding a hashcode to the Command #34117

Closed
ADNewsom09 opened this issue Jun 28, 2024 · 0 comments · Fixed by #34118
Closed

Comments

@ADNewsom09
Copy link
Contributor

ADNewsom09 commented Jun 28, 2024

We found in our application that due to a large number of generated items RelationalCommandCache reached capacity the system would need to do linear searches through MemoryCache's ConcurrentDictionary of CommandCacheKeys due to them all being in the same bucket since there is a hard-coded GetHashCode() of 0. This avoids the advantages of ConcurrentDictionary's O(1) lookup time.

I thought that a simple improvement would be to use RuntimeHelpers.GetHashCode(_queryExpression) instead of 0. This would improve the distribution of buckets in the HashSet while maintaining the contract with the overridden Equals while avoiding any hashcode calculation explosion that might happen if we considered the _parameterValues that are also used in Equals or if an overridden Expression GetHashCode looked at the items in the Expression. as mentioned in #19859.
Since reference equality is the first thing checking in Equals this will be consistent to uniquely find a CommandCacheKey

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