From 5ed783f2cacebbe76fb4e8ffa4442faf6b65ad49 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 12 Oct 2026 20:55:58 +1100 Subject: [PATCH] Pre-size the dictionary in ToDictionary(IEnumerable) Use the count of the source as the capacity when it is a collection, so the dictionary is not grown and rehashed while it is filled. --- .../Polyfill_IEnumerable_ToDictionary.cs | 13 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../Polyfill_IEnumerable_ToDictionary.cs | 11 ++- .../PolyfillTests_IEnumerable_ToDictionary.cs | 87 +++++++++++++++++++ 20 files changed, 279 insertions(+), 19 deletions(-) diff --git a/src/Polyfill/Polyfill_IEnumerable_ToDictionary.cs b/src/Polyfill/Polyfill_IEnumerable_ToDictionary.cs index b7da8ce8..a55ec523 100644 --- a/src/Polyfill/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Polyfill/Polyfill_IEnumerable_ToDictionary.cs @@ -16,7 +16,18 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + // Size the dictionary once when the count is known, instead of growing it while adding + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net461/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net461/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net461/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net461/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net462/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net462/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net462/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net462/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net47/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net47/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net47/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net47/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net471/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net471/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net471/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net471/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net472/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net472/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net472/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net472/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net48/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net48/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net48/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net48/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net481/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net481/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net481/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net481/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net5.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net5.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net5.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net5.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net6.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net6.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net6.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net6.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/net7.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/net7.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/net7.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/net7.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netcoreapp2.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netcoreapp2.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netcoreapp2.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netcoreapp2.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netcoreapp2.1/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netcoreapp2.1/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netcoreapp2.1/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netcoreapp2.1/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netcoreapp2.2/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netcoreapp2.2/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netcoreapp2.2/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netcoreapp2.2/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netcoreapp3.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netcoreapp3.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netcoreapp3.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netcoreapp3.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netcoreapp3.1/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netcoreapp3.1/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netcoreapp3.1/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netcoreapp3.1/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netstandard2.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netstandard2.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netstandard2.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netstandard2.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/netstandard2.1/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/netstandard2.1/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/netstandard2.1/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/netstandard2.1/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Split/uap10.0/Polyfill_IEnumerable_ToDictionary.cs b/src/Split/uap10.0/Polyfill_IEnumerable_ToDictionary.cs index afb22b1f..4b016df8 100644 --- a/src/Split/uap10.0/Polyfill_IEnumerable_ToDictionary.cs +++ b/src/Split/uap10.0/Polyfill_IEnumerable_ToDictionary.cs @@ -13,7 +13,16 @@ public static Dictionary ToDictionary( IEqualityComparer? comparer = null) where TKey : notnull { - var result = new Dictionary(comparer); + var capacity = 0; + if (source is ICollection> collection) + { + capacity = collection.Count; + } + else if (source is IReadOnlyCollection> readOnlyCollection) + { + capacity = readOnlyCollection.Count; + } + var result = new Dictionary(capacity, comparer); foreach (var pair in source) { result.Add(pair.Key, pair.Value); diff --git a/src/Tests/PolyfillTests_IEnumerable_ToDictionary.cs b/src/Tests/PolyfillTests_IEnumerable_ToDictionary.cs index 6ae21b24..670e4dd5 100644 --- a/src/Tests/PolyfillTests_IEnumerable_ToDictionary.cs +++ b/src/Tests/PolyfillTests_IEnumerable_ToDictionary.cs @@ -35,4 +35,91 @@ public async Task IEnumerable_ToDictionary_FromDictionary() await Assert.That(dict["x"]).IsEqualTo(10); await Assert.That(dict["y"]).IsEqualTo(20); } + static IEnumerable> ToDictionaryPairs(int count) + { + for (var index = 0; index < count; index++) + { + yield return new($"key{index}", index); + } + } + + [ExcludeFromCodeCoverage] + [DebuggerNonUserCode] + sealed class ReadOnlyPairs(int count) : + IReadOnlyCollection> + { + public int Count => count; + + public IEnumerator> GetEnumerator() => + ToDictionaryPairs(count).GetEnumerator(); + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => + GetEnumerator(); + } + + [Test] + [Arguments(0)] + [Arguments(1)] + [Arguments(2)] + [Arguments(100)] + [Arguments(5000)] + public async Task IEnumerable_ToDictionary_KeyValuePairs_AllSourceKinds(int count) + { + var expected = ToDictionaryPairs(count).ToList(); + + IEnumerable>[] sources = + [ + expected.ToArray(), + expected, + new Dictionary(expected.ToDictionary(_ => _.Key, _ => _.Value)), + new Queue>(expected), + new ReadOnlyPairs(count), + ToDictionaryPairs(count), + expected.Where(_ => true) + ]; + + foreach (var source in sources) + { + var dictionary = source.ToDictionary(); + await Assert.That(dictionary.Count).IsEqualTo(count); + // items are added in source order, so enumerate in source order + await Assert.That(dictionary.SequenceEqual(expected)).IsTrue(); + await Assert.That(dictionary.Comparer).IsSameReferenceAs(EqualityComparer.Default); + + var withComparer = source.ToDictionary(StringComparer.OrdinalIgnoreCase); + await Assert.That(withComparer.Count).IsEqualTo(count); + await Assert.That(withComparer.Comparer).IsSameReferenceAs(StringComparer.OrdinalIgnoreCase); + if (count > 0) + { + await Assert.That(withComparer[$"KEY{count - 1}"]).IsEqualTo(count - 1); + } + + // the result is a copy that can be added to + dictionary.Add("another", -1); + await Assert.That(dictionary.Count).IsEqualTo(count + 1); + } + } + + [Test] + public async Task IEnumerable_ToDictionary_KeyValuePairs_DuplicateKey_Throws() + { + KeyValuePair[] array = [new("a", 1), new("b", 2), new("a", 3)]; + await Assert.That(() => array.ToDictionary()).Throws(); + await Assert.That(() => array.Where(_ => true).ToDictionary()).Throws(); + + KeyValuePair[] differByCase = [new("a", 1), new("A", 2)]; + await Assert.That(differByCase.ToDictionary().Count).IsEqualTo(2); + await Assert.That(() => differByCase.ToDictionary(StringComparer.OrdinalIgnoreCase)).Throws(); + } + + [Test] + public async Task IEnumerable_ToDictionary_KeyValuePairs_SourceNotModified() + { + var source = new Dictionary {["x"] = 10}; + var copy = source.ToDictionary(); + copy["x"] = 11; + copy["y"] = 20; + await Assert.That(source.Count).IsEqualTo(1); + await Assert.That(source["x"]).IsEqualTo(10); + } }