-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Values with KeyValuePair enumerator in MemoryCache (#53762)
fixes #53761
- Loading branch information
Showing
1 changed file
with
6 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
00ee1c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ezgambac I'm curious to know the motivation behind this change? Is this a sort of optimization or safetly? Both previous and new code do the same...
00ee1c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zyo2012 Iterating over .Values in a concurrent dictionary has 2 issues, it acquires a lock over the whole dictionary, hence blocking new writes, and it copies the .Values to a new list, so a new memory allocation.
Doing it using the iterator over the whole dictionary prevents both issues, as no global lock is acquired, and no new memory is allocated.
00ee1c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ezgambac thanks for the explanation and quick response. Please confirm: So during the iteration of _entries NEW can be added (in a separate thread)?
00ee1c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Afaik, yes