Skip to content

Conversation

@koenigst
Copy link
Contributor

  • Stored the capacity in the ctor.
  • Used the stored capacity in Clear().
  • No tests were added or changed as the capacity logic was only implicitly tested.

Fixes #107016

… sizing the backing array after clearing the collection.

* Stored the capacity in the ctor.
* Used the stored capacity in Clear().

Fixes dotnet#107016
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 20, 2024
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @stephentoub any objections?

@eiriktsarpalis
Copy link
Member

  • No tests were added or changed as the capacity logic was only implicitly tested.

There is precedent in us using reflection to check intentional properties of collections types. Not sure if warranted here, but bare minimum you should try to check if there is test coverage for passing a large-ish capacity parameter and then clearing the collection.

@koenigst
Copy link
Contributor Author

  • No tests were added or changed as the capacity logic was only implicitly tested.

There is precedent in us using reflection to check intentional properties of collections types. Not sure if warranted here, but bare minimum you should try to check if there is test coverage for passing a large-ish capacity parameter and then clearing the collection.

I added a test that checks the following with two custom capacities:

  • Initial table size.
  • Table growth when adding items.
  • Table size after clearing.

106 items might be a bit excessive for a test but the linked issue mentioned this size as being relevant to them.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@eiriktsarpalis eiriktsarpalis merged commit 3bcaadb into dotnet:main Oct 5, 2024
sirntar pushed a commit to sirntar/runtime that referenced this pull request Oct 8, 2024
… sizing the backing array after clearing the collection. (dotnet#108065)

* Stored the initial capacity of the ConcurrentDictionary for correctly sizing the backing array after clearing the collection.

* Stored the capacity in the ctor.
* Used the stored capacity in Clear().

Fixes dotnet#107016

* Added a test to check the capacity logic of the ConcurrentDictionary.
@github-actions github-actions bot locked and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: ConcurrentDictionary.Clear(bool noResize)

3 participants