-
Notifications
You must be signed in to change notification settings - Fork 5.1k
SortedDictionary Copy optimization #45659
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
SortedDictionary Copy optimization #45659
Conversation
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsMy team uses SortedDictionary to store properties in our logging flow, when we start a child activity we often make a copy of the parent properties. I noticed that SortedDictionary constructor from IDictionary always iterates and uses Add. SortedSet which SortedDictionary uses already has an optimization for DeepCloning when the comparer used is the same. By leveraging DeepClone when copying a SortedDictionary with the same comparer there are huge performance benefits especially for larger SortedDictionaries. dotnet/performance@master...johnthcall:sorteddictionarycopy
|
src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs
Outdated
Show resolved
Hide resolved
…c/SortedDictionary.cs Co-authored-by: Jan Kotas <jkotas@microsoft.com>
924991c
to
d1015ed
Compare
...ibraries/System.Collections/tests/Generic/SortedDictionary/SortedDictionary.Generic.Tests.cs
Outdated
Show resolved
Hide resolved
Assert.Equal(sourceSorted, copied); | ||
Assert.Equal(comparer, copied.Comparer); | ||
} | ||
|
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.
Do we have tests for when the collection passed in does not have the same comparer as the one passed in? For both IDictionary<K,V> and SortedDictionary<K,V> ? I don't see them.
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.
Dictionary classes only have constructors with IEqualityComparer not IComparer. In SortedDictionary are we okay using a inline comparer which return negative of what GetKeyIComparer returns?
src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs
Outdated
Show resolved
Hide resolved
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.
Thank you for your contribution!
My team uses SortedDictionary to store properties in our logging flow, when we start a child activity we often make a copy of the parent properties. I noticed that SortedDictionary constructor from IDictionary always iterates and uses Add. SortedSet which SortedDictionary uses already has an optimization for DeepCloning when the comparer used is the same. By leveraging DeepClone when copying a SortedDictionary with the same comparer there are huge performance benefits especially for larger SortedDictionaries.
dotnet/performance@master...johnthcall:sorteddictionarycopy