Is hashcode comparison necessary in DefaultFrozenDictionary GetValueRefOrNullRefCore? #107878
-
DescriptionI looked at the code of frozen dictionaries. In the DefaultFrozenDictionary class, when we receive a reference to an object, we get the first/last indexes of the keys from the FrozenHashTable. Should the keys for these indexes have the same hash codes? If yes, then additional comparison of hash codes in the frozen dictionary is not necessary. This also applies to ValueTypeDefaultComparerFrozenDictionary and probably other implementations. Code
Links |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Tagging subscribers to this area: @dotnet/area-system-collections |
Beta Was this translation helpful? Give feedback.
-
While the implementation tries to reduce the number of collisions when mapping hash tables to buckets, it doesn't guarantee a 1:1 ratio. As such, the same bucket can contain entries with different hash codes. And as it's generally much cheaper to compare an int than to do the equality comparison for a key, eliminating the hash code can show up as a regression. It's there to prevent that. |
Beta Was this translation helpful? Give feedback.
While the implementation tries to reduce the number of collisions when mapping hash tables to buckets, it doesn't guarantee a 1:1 ratio. As such, the same bucket can contain entries with different hash codes. And as it's generally much cheaper to compare an int than to do the equality comparison for a key, eliminating the hash code can show up as a regression. It's there to prevent that.