From 1bc95a290f47b64492d9fa9a69df5ddd345b2150 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 29 Jan 2022 20:18:49 +0000 Subject: [PATCH] Preallocate dictionary size in ToDictionaryWithConflictLog --- OpenRA.Game/Exts.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/OpenRA.Game/Exts.cs b/OpenRA.Game/Exts.cs index b579585250..6e3cc6038f 100644 --- a/OpenRA.Game/Exts.cs +++ b/OpenRA.Game/Exts.cs @@ -407,7 +407,8 @@ namespace OpenRA // Try to build a dictionary and log all duplicates found (if any): var dupKeys = new Dictionary>(); - var d = new Dictionary(); + var capacity = source is ICollection collection ? collection.Count : 0; + var d = new Dictionary(capacity); foreach (var item in source) { var key = keySelector(item); @@ -418,7 +419,7 @@ namespace OpenRA continue; // Check for a key conflict: - if (d.ContainsKey(key)) + if (!d.TryAdd(key, element)) { if (!dupKeys.TryGetValue(key, out var dupKeyMessages)) { @@ -430,10 +431,7 @@ namespace OpenRA // Log this conflicting value: dupKeyMessages.Add(logValue(element)); - continue; } - - d.Add(key, element); } // If any duplicates were found, throw a descriptive error