Saturday, 28 September 2013

Optimal performance of Dictionary with custom Equals() and GetHashCode()

Optimal performance of Dictionary with custom Equals() and GetHashCode()

So I need to create a dictionary with keys that are objects with a custom
Equals() function. I discovered I need to override GetHashCode() too. I
heard that for optimal performance you should have hash codes that don't
collide, but that seems counter intuitive. I might be misunderstanding it,
but it seems the entire point of using hash codes is to group items into
buckets and if the hash codes never collide each bucket will only have 1
item which seems to defeat the purpose.
So should I intentionally make my hash codes collide occasionally?
Performance is important. This will be a dictionary that will probably
grow to multiple million items and I'll be doing lookups very often.

No comments:

Post a Comment