Dict: Use Ref Local Reassignment (C# 7.3) (#17483)
authorBen Adams <thundercat@illyriad.co.uk>
Tue, 17 Apr 2018 01:02:38 +0000 (02:02 +0100)
committerJan Kotas <jkotas@microsoft.com>
Tue, 17 Apr 2018 01:02:38 +0000 (18:02 -0700)
* Dict: Use Ref Local Reassignment (C# 7.3)

* Use lang version latest rather than 7.3

src/mscorlib/System.Private.CoreLib.csproj
src/mscorlib/shared/System/Collections/Generic/Dictionary.cs

index 3a0c6b6..f5d6064 100644 (file)
@@ -39,7 +39,7 @@
     <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)/Documentation</_TargetFrameworkDirectories>
     <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)/Documentation</_FullFrameworkReferenceAssemblyPaths>
     <SkipCommonResourcesIncludes>true</SkipCommonResourcesIncludes>
-    <LangVersion>7.2</LangVersion>
+    <LangVersion>latest</LangVersion>
   </PropertyGroup>
   <!-- Add Serviceable attribute to the project's metadata -->
   <ItemGroup>
index d76df40..da5d8bb 100644 (file)
@@ -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)