[API Proposal]: ConcurrentDictionary.Clear(bool noResize) #107016
Closed
Description
opened on Aug 27, 2024
Background and motivation
ConcurrentDictionary<TKey, TValue>
allows setting the initial size; however when you call .Clear() it resets it to size 31
This 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
namespace System.Collections.Generic;
public class ConcurrentDictionary<TKey, TValue>
{
public void Clear(bool noResize);
}
If noResize
is true it should use the current length of _tables._buckets.Length
rather than HashHelpers.GetPrime(DefaultCapacity)
when recreating the arrays
API Usage
// Fancy the value
var c = new ConcurrentDictionary<long, long>(concurrencyLevel: Environment.ProcessorCount * 16, capacity: 4_000_000);
c.Clear(noResize: true);
Alternative Designs
No response
Risks
No response
Activity