1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-14 14:24:10 +02:00

Better hashcode bit reduction

This commit is contained in:
Brian Fiete 2024-08-14 16:42:31 -04:00
parent 78e9716f3a
commit 9f94d4588f
2 changed files with 16 additions and 13 deletions

View file

@ -365,7 +365,7 @@ namespace System.Collections
if (sizeof(int_cosize) == 8) if (sizeof(int_cosize) == 8)
return (int_cosize)(hashCode & 0x7FFFFFFF'FFFFFFFFL); return (int_cosize)(hashCode & 0x7FFFFFFF'FFFFFFFFL);
#unwarn #unwarn
return ((int32)hashCode ^ (int32)((int64)hashCode >> 33)) & 0x7FFFFFFF; return (int32)(hashCode ^ ((hashCode >> 31) * 1171)) & 0x7FFFFFFF;
} }
[DisableObjectAccessChecks] [DisableObjectAccessChecks]

View file

@ -182,7 +182,7 @@ namespace System.Collections
{ {
if (mBuckets != null) if (mBuckets != null)
{ {
int32 hashCode = (int32)InternalGetHashCode(item); int32 hashCode = InternalGetHashCode(item);
// see note at "HashSet" level describing why "- 1" appears in for loop // see note at "HashSet" level describing why "- 1" appears in for loop
for (int32 i = mBuckets[hashCode % mBuckets.Count] - 1; i >= 0; i = mSlots[i].mNext) for (int32 i = mBuckets[hashCode % mBuckets.Count] - 1; i >= 0; i = mSlots[i].mNext)
{ {
@ -277,7 +277,7 @@ namespace System.Collections
{ {
if (mBuckets != null) if (mBuckets != null)
{ {
int32 hashCode = (int32)InternalGetHashCode(item); int32 hashCode = InternalGetHashCode(item);
int32 bucket = hashCode % (int32)mBuckets.Count; int32 bucket = hashCode % (int32)mBuckets.Count;
int32 last = -1; int32 last = -1;
for (int32 i = mBuckets[bucket] - 1; i >= 0; last = i,i = mSlots[i].mNext) for (int32 i = mBuckets[bucket] - 1; i >= 0; last = i,i = mSlots[i].mNext)
@ -616,7 +616,7 @@ namespace System.Collections
{ {
if (newSlots[i].mHashCode != -1) if (newSlots[i].mHashCode != -1)
{ {
newSlots[i].mHashCode = (int32)InternalGetHashCode(newSlots[i].mValue); newSlots[i].mHashCode = InternalGetHashCode(newSlots[i].mValue);
} }
} }
} }
@ -645,7 +645,7 @@ namespace System.Collections
Initialize(0); Initialize(0);
} }
int32 hashCode = (int32)InternalGetHashCode(value); int32 hashCode = InternalGetHashCode(value);
int32 bucket = hashCode % (int32)mBuckets.Count; int32 bucket = hashCode % (int32)mBuckets.Count;
#if FEATURE_RANDOMIZED_STRING_HASHING && !FEATURE_NETCORE #if FEATURE_RANDOMIZED_STRING_HASHING && !FEATURE_NETCORE
int collisionCount = 0; int collisionCount = 0;
@ -1182,23 +1182,26 @@ namespace System.Collections
}*/ }*/
/// Workaround Comparers that throw ArgumentNullException for GetHashCode(null). /// Workaround Comparers that throw ArgumentNullException for GetHashCode(null).
/// @return hash code /// @return hash codeInternalGetHashCode
private int InternalGetHashCode(T item) public int32 GetHashKey(int hashCode)
{
if (sizeof(int) == 4)
return (int32)hashCode;
return (int32)(hashCode ^ ((hashCode >> 31) * 1171)) & 0x7FFFFFFF;
}
private int32 InternalGetHashCode(T item)
{ {
if (item == null) if (item == null)
{
return 0; return 0;
} return GetHashKey(item.GetHashCode());
return item.GetHashCode() & Lower31BitMask;
} }
private int InternalGetHashCodeAlt<TAltKey>(TAltKey item) where TAltKey : IHashable private int InternalGetHashCodeAlt<TAltKey>(TAltKey item) where TAltKey : IHashable
{ {
if (item == null) if (item == null)
{
return 0; return 0;
} return GetHashKey(item.GetHashCode());
return item.GetHashCode() & Lower31BitMask;
} }
#endregion #endregion