Preallocate dictionary size in ToDictionaryWithConflictLog

This commit is contained in:
RoosterDragon
2022-01-29 20:18:49 +00:00
committed by abcdefg30
parent 74cced319c
commit 1bc95a290f

View File

@@ -407,7 +407,8 @@ namespace OpenRA
// Try to build a dictionary and log all duplicates found (if any): // Try to build a dictionary and log all duplicates found (if any):
var dupKeys = new Dictionary<TKey, List<string>>(); var dupKeys = new Dictionary<TKey, List<string>>();
var d = new Dictionary<TKey, TElement>(); var capacity = source is ICollection<TSource> collection ? collection.Count : 0;
var d = new Dictionary<TKey, TElement>(capacity);
foreach (var item in source) foreach (var item in source)
{ {
var key = keySelector(item); var key = keySelector(item);
@@ -418,7 +419,7 @@ namespace OpenRA
continue; continue;
// Check for a key conflict: // Check for a key conflict:
if (d.ContainsKey(key)) if (!d.TryAdd(key, element))
{ {
if (!dupKeys.TryGetValue(key, out var dupKeyMessages)) if (!dupKeys.TryGetValue(key, out var dupKeyMessages))
{ {
@@ -430,10 +431,7 @@ namespace OpenRA
// Log this conflicting value: // Log this conflicting value:
dupKeyMessages.Add(logValue(element)); dupKeyMessages.Add(logValue(element));
continue;
} }
d.Add(key, element);
} }
// If any duplicates were found, throw a descriptive error // If any duplicates were found, throw a descriptive error