Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Improve Dictionary FindEntry CQ #15460

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/mscorlib/shared/System/Collections/Generic/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,28 @@ private int FindEntry(TKey key)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}

if (_buckets != null)
int[] buckets = _buckets;
int i = -1;
if (buckets != null)
{
int hashCode = _comparer.GetHashCode(key) & 0x7FFFFFFF;
for (int i = _buckets[hashCode % _buckets.Length]; i >= 0; i = _entries[i].next)
IEqualityComparer<TKey> comparer = _comparer;
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
i = buckets[hashCode % buckets.Length];

Entry[] entries = _entries;
do
{
if (_entries[i].hashCode == hashCode && _comparer.Equals(_entries[i].key, key)) return i;
}
// Should be a while loop https://github.com/dotnet/coreclr/issues/15476
// Test in if to drop range check for following array access
if ((uint)i >= (uint)entries.Length || (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)))
{
break;
}

i = entries[i].next;
} while (true);
}
return -1;
return i;
}

private void Initialize(int capacity)
Expand Down