Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added condition of empty for concurrent dictionary #44581

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ public void Clear()
{
AcquireAllLocks(ref locksAcquired);

// If the dictionary is already empty, then there's nothing to clear.
if (AreAllBucketsEmpty())
{
return;
}

Tables tables = _tables;
var newTables = new Tables(new Node[DefaultCapacity], tables._locks, new int[tables._countPerLock.Length]);
_tables = newTables;
Expand Down Expand Up @@ -1421,20 +1427,7 @@ public bool IsEmpty
ReleaseLocks(0, acquiredLocks);
}

bool AreAllBucketsEmpty()
{
int[] countPerLock = _tables._countPerLock;

for (int i = 0; i < countPerLock.Length; i++)
{
if (countPerLock[i] != 0)
{
return false;
}
}

return true;
}
}
}

Expand Down Expand Up @@ -1874,6 +1867,22 @@ void ICollection.CopyTo(Array array, int index)

#endregion


private bool AreAllBucketsEmpty()
{
int[] countPerLock = _tables._countPerLock;

for (int i = 0; i < countPerLock.Length; i++)
{
if (countPerLock[i] != 0)
{
return false;
}
}

return true;
}

/// <summary>
/// Replaces the bucket table with a larger one. To prevent multiple threads from resizing the
/// table as a result of races, the Tables instance that holds the table of buckets deemed too
Expand Down