ice icon indicating copy to clipboard operation
ice copied to clipboard

Dictionary equal and hashing in C#

Open bernardnormier opened this issue 1 year ago • 1 comments

In C#, we check if two dictionaries are equal using:

https://github.com/zeroc-ice/ice/blob/711bb2939294bb1dbb940e3a4a6966e89c5ed735/csharp/src/Ice/Collections.cs#L103

The algorithm assumes the two dictionaries are sorted in the same order. This is not the behavior you would expect since it depends on the IDictionary implementation. Two implementations with the same contents can easily compare different.

IceRPC C# provides an order-independent implementation of DictionaryEqual: https://github.com/icerpc/icerpc-csharp/blob/57995b702436169798fc3fe5fd513e6f09591397/src/IceRpc/Internal/DictionaryExtensions.cs#L10

Ice C# also provides HashCode computation for these dictionaries that is also order-dependent (that's consistent with Equal, which is good). IceRPC doesn't provide a HashCode implementation. It's not obvious how to provide a correct order-independent HashCode implementation. A simple solution is to hash the size of the dictionary, not its contents.

bernardnormier avatar May 07 '24 14:05 bernardnormier

Another option is to do like Java: the hash code for a Map / Set is the sum of the hash code of all elements: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Set.html#hashCode()

bernardnormier avatar May 07 '24 15:05 bernardnormier

Fixed: we no longer use "entry hashing" for any dictionary in C#.

bernardnormier avatar May 31 '24 19:05 bernardnormier