Skip to content

Commit 14c1d48

Browse files
AndrewP-GHCopilotMihaZupan
authored
Pre-size intermediate Dictionary in ToFrozenDictionary (#128300)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
1 parent 1753549 commit 14c1d48

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey,
131131
newDictionary = source as Dictionary<TKey, TValue>;
132132
if (newDictionary is null || (newDictionary.Count != 0 && !newDictionary.Comparer.Equals(comparer)))
133133
{
134-
newDictionary = new Dictionary<TKey, TValue>(comparer);
134+
int capacity = (source as ICollection<KeyValuePair<TKey, TValue>>)?.Count ?? 0;
135+
newDictionary = new Dictionary<TKey, TValue>(capacity, comparer);
135136
foreach (KeyValuePair<TKey, TValue> pair in source)
136137
{
137138
// Dictionary's constructor uses Add, which will throw on duplicates.

0 commit comments

Comments
 (0)