{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/3357","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/3357/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/3357/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/3357/events","html_url":"https://github.com/dotnet/corefx/issues/3357","id":107613341,"number":3357,"title":"ConcurrentDictionary: Count property is too expensive","user":{"login":"ikopylov","id":10262823,"avatar_url":"https://avatars.githubusercontent.com/u/10262823?v=3","gravatar_id":"","url":"https://api.github.com/users/ikopylov","html_url":"https://github.com/ikopylov","followers_url":"https://api.github.com/users/ikopylov/followers","following_url":"https://api.github.com/users/ikopylov/following{/other_user}","gists_url":"https://api.github.com/users/ikopylov/gists{/gist_id}","starred_url":"https://api.github.com/users/ikopylov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ikopylov/subscriptions","organizations_url":"https://api.github.com/users/ikopylov/orgs","repos_url":"https://api.github.com/users/ikopylov/repos","events_url":"https://api.github.com/users/ikopylov/events{/privacy}","received_events_url":"https://api.github.com/users/ikopylov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-21T22:46:35Z","updated_at":"2015-09-21T22:55:47Z","closed_at":null,"body":"In the current implementation of ```Count``` property from ```ConcurrentDictionary<TKey, TValue>``` the full lock is acquired, which is quite expensive. It is much better to add a private field to track element count in the whole dictionary and update it by ```Interlocked``` operations.\r\n\r\nIt would be great if someone will implement this performance tweak."},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/142131725","html_url":"https://github.com/dotnet/corefx/issues/3357#issuecomment-142131725","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/3357","id":142131725,"user":{"login":"stephentoub","id":2642209,"avatar_url":"https://avatars.githubusercontent.com/u/2642209?v=3","gravatar_id":"","url":"https://api.github.com/users/stephentoub","html_url":"https://github.com/stephentoub","followers_url":"https://api.github.com/users/stephentoub/followers","following_url":"https://api.github.com/users/stephentoub/following{/other_user}","gists_url":"https://api.github.com/users/stephentoub/gists{/gist_id}","starred_url":"https://api.github.com/users/stephentoub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephentoub/subscriptions","organizations_url":"https://api.github.com/users/stephentoub/orgs","repos_url":"https://api.github.com/users/stephentoub/repos","events_url":"https://api.github.com/users/stephentoub/events{/privacy}","received_events_url":"https://api.github.com/users/stephentoub/received_events","type":"User","site_admin":false},"created_at":"2015-09-21T22:55:47Z","updated_at":"2015-09-21T22:55:47Z","body":">  It is much better to add a private field to track element count in the whole dictionary and update it by Interlocked operations\r\n\r\nIt's not, though.  When many threads are mutating the dictionary, those interlocked operations on a single shared field are going to cause contention across all of those threads, slowing down adds/removes/updates/etc; the design of ConcurrentDictionary is such that different threads typically end up taking different locks, such that this contention is eliminated, and we chose to prioritize such operations rather than the typically comparatively rare act of accessing the Count."}}