-
Notifications
You must be signed in to change notification settings - Fork 933
Fix CollectionHelper.GetHashCode ignoring values of dictionaries #1613
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
Fix CollectionHelper.GetHashCode ignoring values of dictionaries #1613
Conversation
a08d4e6
to
506c026
Compare
I would like this to go to master first. Need to have a separate unit test for the method. |
Actually, do not want this change. The |
Yes it is not required by So when a query is cached for many different parameter values, its lookup in cache is an O(n) operation due to the current implementation (with n being the number of different parameter values). |
506c026
to
deb7d1c
Compare
/// individual elements key xored with their value if any, so that the value is independent of the | ||
/// collection iteration order. | ||
/// </remarks> | ||
public static int GetHashCode<TK, TV>(IEnumerable<KeyValuePair<TK, TV>> coll) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better to make an overload that accepts IEqualityComparer<T>
and KeyValuePairComparer<TK, TV>
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overload added here is a specialization of the existing public static int GetHashCode<T>(IEnumerable<T> coll)
, allowing to handle the dictionary case specifically without source changes on the GetHashCode
callers side: with a recompilation, when they supply a dictionary, that is this new overload which gets called, allowing to fix the issue.
Adding comparer overloads would require to change the calling code too (8 cases, in cache, engine and mapping namespaces). I would rather see this as a separate refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this fix is too specialised. The problem exists for all struct types which can contain references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix matches what NHibernate currently uses. CollectionHelper
shoudn't be public in the first place but internal, and shouldn't aim at being generic but tailored to what NHibernate needs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this method internal then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CollectionHelper in the current form shall die. I propose to replace it with a set of content equality comparers (I will probably prepare a PR). |
I agree. Nonetheless it should not prevent fixing its issues while it is still alive. So unless the switch to equality comparer can be done soon, better still fix its current implementation. |
Maybe it's better to add a long overdue |
This would require strong signing the test assembly too, because for assemblies to be friend, either both are unsigned or both are signed. An unsigned one cannot be friend of a signed one. |
I've re-checked the code.
|
Replaced by #1694 |
This is causing
FilterKey
andQueryKey
for the same queries but having different parameter values to always have the same hashcode, which is inefficient for keys lookup.This PR is build over #1612, which includes the first four commits we can currently see here.