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 bypass #11212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

SimaTian
Copy link
Member

@SimaTian SimaTian commented Jan 3, 2025

profiler was repeatedly complaining about ConcurrentDictionary.Count taking locks too often.
This PR introduces an approximate counter to remedy that

Context

concurrentDictionary
ConcurrentDictionary.Count locks all its internal locks to have the count accurate and up to date.
However we only use the count to check if there is a reason to clean up the cache - e.g. we should be fine with a variable that is almost-in-sync with the .Count, that we can update atomically and then read without locking.
The increment is atomic, the read is accurate enough and the "flush cache" section is already behind a lock.

Changes Made

Introduced _count variable that lists the same value as ConcurrentDictionary.Count would.

Testing

Now that I looked at this, we didn't have a test for the scavenge threshold. We only ever tested the .scavenge directly.
Do we want to remedy that or are we fine as is?

…taking locks too often. This PR introduces an approximate counter to remedy that
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant