Improve Dict.Clear CQ (#15459)
authorBen Adams <thundercat@illyriad.co.uk>
Mon, 11 Dec 2017 02:42:54 +0000 (02:42 +0000)
committerJan Kotas <jkotas@microsoft.com>
Mon, 11 Dec 2017 02:42:54 +0000 (21:42 -0500)
src/mscorlib/shared/System/Collections/Generic/Dictionary.cs

index 5771805..6bd0785 100644 (file)
@@ -48,9 +48,9 @@ namespace System.Collections.Generic
         private int[] _buckets;
         private Entry[] _entries;
         private int _count;
-        private int _version;
         private int _freeList;
         private int _freeCount;
+        private int _version;
         private IEqualityComparer<TKey> _comparer;
         private KeyCollection _keys;
         private ValueCollection _values;
@@ -256,14 +256,20 @@ namespace System.Collections.Generic
 
         public void Clear()
         {
-            if (_count > 0)
+            int count = _count;
+            if (count > 0)
             {
-                for (int i = 0; i < _buckets.Length; i++) _buckets[i] = -1;
-                Array.Clear(_entries, 0, _count);
-                _freeList = -1;
+                int[] buckets = _buckets;
+                for (int i = 0; i < buckets.Length; i++)
+                {
+                    buckets[i] = -1;
+                }
+
                 _count = 0;
+                _freeList = -1;
                 _freeCount = 0;
                 _version++;
+                Array.Clear(_entries, 0, count);
             }
         }