Commit cbeb324
committed
Pre-size intermediate Dictionary in ToFrozenDictionary
When the source is not already a Dictionary, ToFrozenDictionary builds
an intermediate Dictionary to deduplicate keys with last-wins semantics.
The previous code used the default-capacity constructor, which paid
log2(N) resizes and ~2N redundant rehashes when populating from a known
size source such as an array or List.
Use ICollection<KeyValuePair>.Count as a capacity hint when available,
mirroring the HashSet(IEnumerable, IEqualityComparer) constructor.
The ReadOnlySpan overload of FrozenDictionary.Create already pre-sizes,
so this brings the IEnumerable path in line with it.1 parent f288fd5 commit cbeb324
1 file changed
Lines changed: 2 additions & 1 deletion
File tree
- src/libraries/System.Collections.Immutable/src/System/Collections/Frozen
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
| 135 | + | |
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
| |||
0 commit comments