-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve FrozenDictionary.ToFrozenDictionary for Dictionary sources with different comparers
#120795
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
Open
prozolic
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
prozolic:FrozenDictionary.ToFrozenDictionary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+13
−1
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b26d19
Improve FrozenDictionary.ToFrozenDictionary for Dictionary sources wi…
prozolic 9fff89d
Fix file encoding to UTF-8 with BOM
prozolic 089e68b
Remove initial capacity specification
prozolic e2dbcf6
Merge branch 'main' into FrozenDictionary.ToFrozenDictionary
prozolic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could end up significantly over-allocating, no? Let's say the original use an Ordinal comparer, the new uses an OrdinalIgnoreCase comparer, and the original had every string entry copied many times just with different casing. The resulting dictionary's count is going to be much smaller than the originals, yet this will cause it to allocate much more memory?
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.
@stephentoub You're correct. Pre-allocating the dictionary with the original size may result in over-allocating when duplicate keys reduce the required capacity. I have removed the initial capacity specification.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks. At that point then, is the only difference this new code path adds is iterating over a
Dictionary<>in a strongly-typed way rather than iterating over a dictionary viaIEnumerable<KeyValuePair<>>? In this state, have you tried running the before/after benchmarks on .NET 10?Uh oh!
There was an error while loading. Please reload this page.
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.
I've updated the benchmark results accordingly.
Uh oh!
There was an error while loading. Please reload this page.
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.
@stephentoub from the updated benchmark result I see the original one that reserves the initial capacity allocated far less than the new one that doesn't do it. Instead of letting the backing storage to resizing its size to 2x each time (which would also result in over-allocating), pre-allocating the capacity that may be a bit more than the actual need seems definite more feasible to me.
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.
It's going to depend on how much of a difference is expected in count from the original collection that's using a different comparer. The benchmark is using a comparer with identical semantics, so the ideal is to just use the exact same capacity. If, however and for example, the original collection was using Ordinal, the new collection was using OrdinalIgnoreCase, and the original collection contained 1000 words each with 10 different casing variations, choosing that original count is going to result in 10x the allocation required whereas the normal growth strategy will only be 2x.
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.
I initially suggested pre-allocating, but after considering @stephentoub's concern about the worst-case scenario, I agree that not setting the initial capacity is the better approach.
Since the optimal capacity depends on on the comparer's behavior, the dynamic growth strategy is more robust.