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

Commit c8db268

Browse files
benaadamsjkotas
authored andcommitted
Improve Dict.Clear CQ (#15459)
1 parent 1724c75 commit c8db268

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/mscorlib/shared/System/Collections/Generic/Dictionary.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ private struct Entry
4848
private int[] _buckets;
4949
private Entry[] _entries;
5050
private int _count;
51-
private int _version;
5251
private int _freeList;
5352
private int _freeCount;
53+
private int _version;
5454
private IEqualityComparer<TKey> _comparer;
5555
private KeyCollection _keys;
5656
private ValueCollection _values;
@@ -256,14 +256,20 @@ bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> k
256256

257257
public void Clear()
258258
{
259-
if (_count > 0)
259+
int count = _count;
260+
if (count > 0)
260261
{
261-
for (int i = 0; i < _buckets.Length; i++) _buckets[i] = -1;
262-
Array.Clear(_entries, 0, _count);
263-
_freeList = -1;
262+
int[] buckets = _buckets;
263+
for (int i = 0; i < buckets.Length; i++)
264+
{
265+
buckets[i] = -1;
266+
}
267+
264268
_count = 0;
269+
_freeList = -1;
265270
_freeCount = 0;
266271
_version++;
272+
Array.Clear(_entries, 0, count);
267273
}
268274
}
269275

0 commit comments

Comments
 (0)