Commit 1f15afe
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.
Adds MultipleValuesSameKey_ICollectionSource_LastInWins regression test
to guard last-wins semantics over the pre-sized path.1 parent f288fd5 commit 1f15afe
2 files changed
Lines changed: 27 additions & 1 deletion
File tree
- src/libraries/System.Collections.Immutable
- src/System/Collections/Frozen
- tests/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 | | |
| |||
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
337 | 362 | | |
338 | 363 | | |
339 | 364 | | |
| |||
0 commit comments