-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: ConcurrentDictionary.Clear(bool noResize) #107016
Comments
Tagging subscribers to this area: @dotnet/area-system-collections |
Could we entertain the possibility of making this the default behaviour for |
I'd be happy with that |
We would probably need to add corresponding |
Will the breaking change actually break anything other than GC timing? |
I believe it'd be ~14 to get up to 1,000,000.
ConcurrentDictionary is different. It has to allocate a new array as part of Clear, which is not the case for Dictionary, and why it prefers to allocate one of the default size rather than the current size, since it has no information to suggest that the capacity after clearing will get to be anywhere close to where it was. For the same reason, I'm not a fan of "noResize"; that would imply it's somehow able to keep the current capacity, but it's not. I'd be ok considering storing the initial user provided capacity if one was provided and having Clear prefer to use that. But I don't think we should just have Clear always allocate a new array of length equal to the current array's length. |
@benaadams would this address your use case? |
Yes |
… sizing the backing array after clearing the collection. * Stored the capacity in the ctor. * Used the stored capacity in Clear(). Fixes dotnet#107016
Background and motivation
ConcurrentDictionary<TKey, TValue>
allows setting the initial size; however when you call .Clear() it resets it to size 31This is a problem if your expected size is 1 million; and it now goes through very many increasingly expensive resizes getting back to its previous size
API Proposal
If
noResize
is true it should use the current length of_tables._buckets.Length
rather thanHashHelpers.GetPrime(DefaultCapacity)
when recreating the arraysruntime/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs
Lines 723 to 726 in 9a31a5b
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: