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

ConcurrentDictionary Count is slow #100853

Closed
fredwangwang opened this issue Apr 10, 2024 · 3 comments
Closed

ConcurrentDictionary Count is slow #100853

fredwangwang opened this issue Apr 10, 2024 · 3 comments
Labels

Comments

@fredwangwang
Copy link

Description

During one of the performance testing, found out that calling to ConcurrentDictionary.Count is contributing significant time to a hot path.

Configuration

win x64 .net8.0

Regression?

Dont think this is a regression.

Analysis

Looking into the implementation for Count, found out that each time Count is called, it is locking the entire dictionary and iterate through all buckets to get a sum.

It seems that the performance would be much better to keep an atomic counter so that get Count is instant.

https://source.dot.net/#System.Collections.Concurrent/System/Collections/Concurrent/ConcurrentDictionary.cs,1116

image

image

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 10, 2024
@EgorBo
Copy link
Member

EgorBo commented Apr 10, 2024

See #15249 (comment)

@eiriktsarpalis
Copy link
Member

Duplicate of #15249

@eiriktsarpalis eiriktsarpalis marked this as a duplicate of #15249 Apr 10, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants