From c9c401b81d7bdeb3d5932df117266caa0da5c32c Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 17 Apr 2018 02:02:38 +0100 Subject: [PATCH] Dict: Use Ref Local Reassignment (C# 7.3) (#17483) * Dict: Use Ref Local Reassignment (C# 7.3) * Use lang version latest rather than 7.3 --- src/mscorlib/System.Private.CoreLib.csproj | 2 +- .../shared/System/Collections/Generic/Dictionary.cs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 3a0c6b6265..f5d60647fd 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -39,7 +39,7 @@ <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)/Documentation <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)/Documentation true - 7.2 + latest diff --git a/src/mscorlib/shared/System/Collections/Generic/Dictionary.cs b/src/mscorlib/shared/System/Collections/Generic/Dictionary.cs index d76df4041a..da5d8bb86b 100644 --- a/src/mscorlib/shared/System/Collections/Generic/Dictionary.cs +++ b/src/mscorlib/shared/System/Collections/Generic/Dictionary.cs @@ -613,9 +613,6 @@ namespace System.Collections.Generic } - // Can be improved with "Ref Local Reassignment" - // https://github.com/dotnet/csharplang/blob/master/proposals/ref-local-reassignment.md - bool resized = false; bool updateFreeList = false; int index; if (_freeCount > 0) @@ -630,14 +627,13 @@ namespace System.Collections.Generic if (count == entries.Length) { Resize(); - resized = true; + bucket = ref _buckets[hashCode % _buckets.Length]; } index = count; _count = count + 1; entries = _entries; } - ref int targetBucket = ref resized ? ref _buckets[hashCode % _buckets.Length] : ref bucket; ref Entry entry = ref entries[index]; if (updateFreeList) @@ -646,11 +642,11 @@ namespace System.Collections.Generic } entry.hashCode = hashCode; // Value in _buckets is 1-based - entry.next = targetBucket - 1; + entry.next = bucket - 1; entry.key = key; entry.value = value; // Value in _buckets is 1-based - targetBucket = index + 1; + bucket = index + 1; // Value types never rehash if (default(TKey) == null && collisionCount > HashHelpers.HashCollisionThreshold && comparer is NonRandomizedStringEqualityComparer) -- 2.34.1