Performance Quiz #12 -- The Cost of a Good Hash
This quiz is a little bit different than some of my others. Here is System.String.GetHashCode
public override unsafe int GetHashCode()
{
fixed (char* text1 = ((char*) this))
{
char* chPtr1 = text1;
int num1 = 0x15051505;
int num2 = num1;
int* numPtr1 = (int*) chPtr1;
for (int num3 = this.Length; num3 > 0; num3 -= 4)
{
num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
if (num3 <= 2)
{
break;
}
num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
numPtr1 += 2;
}
return (num1 + (num2 * 0x5d588b65));
}
}
It's a fine little hash function that's does pretty much the sorts of things you'd expect a hash function to do.
So now the quiz. It's a bit of trivia but I'm going somewhere with it.
Can you tell me 5 implementations of GetHashCode in the framework that do things that you wouldn't expect a hashing function to do?