diff options
author | Ben Adams <thundercat@illyriad.co.uk> | 2017-12-11 05:19:19 +0300 |
---|---|---|
committer | Ahson Khan <ahkha@microsoft.com> | 2018-03-02 07:26:31 +0300 |
commit | 8c6b8b640915d384d8f72bf2ca9b1068ab749448 (patch) | |
tree | 90ca31d67864045c566b53953e9fea81fc541863 | |
parent | 190c12208f41b35718087f6775a7bcb390d266a6 (diff) |
Improve Dictionary FindEntry CQ
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Collections/Generic/Dictionary.cs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Collections/Generic/Dictionary.cs b/src/System.Private.CoreLib/shared/System/Collections/Generic/Dictionary.cs index 1b89158a9..72c2a32d7 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Generic/Dictionary.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Generic/Dictionary.cs @@ -362,15 +362,28 @@ namespace System.Collections.Generic 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 int Initialize(int capacity) |