Skip to content

Commit

Permalink
Special-case array/list in Dictionary(IEnumerable, …) ctors (#86254)
Browse files Browse the repository at this point in the history
* Special-case array/list in Dictionary(IEnumerable, ...) ctors

It's reasonably common to construct a dictionary from a `KeyValuePair<TKey, TValue>[]` or a `List<KeyValuePair<TKey, TValue>>`, and we can make both cheaper.
  • Loading branch information
stephentoub authored May 17, 2023
1 parent 7c72ba5 commit 709444f
Showing 1 changed file with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

namespace System.Collections.Generic
Expand Down Expand Up @@ -83,7 +85,7 @@ public Dictionary(int capacity, IEqualityComparer<TKey>? comparer)
public Dictionary(IDictionary<TKey, TValue> dictionary) : this(dictionary, null) { }

public Dictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer) :
this(dictionary != null ? dictionary.Count : 0, comparer)
this(dictionary?.Count ?? 0, comparer)
{
if (dictionary == null)
{
Expand All @@ -106,15 +108,15 @@ public Dictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityC
AddRange(collection);
}

private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> collection)
private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> enumerable)
{
// It is likely that the passed-in dictionary is Dictionary<TKey,TValue>. When this is the case,
// It is likely that the passed-in enumerable is Dictionary<TKey,TValue>. When this is the case,
// avoid the enumerator allocation and overhead by looping through the entries array directly.
// We only do this when dictionary is Dictionary<TKey,TValue> and not a subclass, to maintain
// back-compat with subclasses that may have overridden the enumerator behavior.
if (collection.GetType() == typeof(Dictionary<TKey, TValue>))
if (enumerable.GetType() == typeof(Dictionary<TKey, TValue>))
{
Dictionary<TKey, TValue> source = (Dictionary<TKey, TValue>)collection;
Dictionary<TKey, TValue> source = (Dictionary<TKey, TValue>)enumerable;

if (source.Count == 0)
{
Expand Down Expand Up @@ -150,8 +152,29 @@ private void AddRange(IEnumerable<KeyValuePair<TKey, TValue>> collection)
return;
}

// Fallback path for IEnumerable that isn't a non-subclassed Dictionary<TKey,TValue>.
foreach (KeyValuePair<TKey, TValue> pair in collection)
// We similarly special-case KVP<>[] and List<KVP<>>, as they're commonly used to seed dictionaries, and
// we want to avoid the enumerator costs (e.g. allocation) for them as well. Extract a span if possible.
ReadOnlySpan<KeyValuePair<TKey, TValue>> span;
if (enumerable is KeyValuePair<TKey, TValue>[] array)
{
span = array;
}
else if (enumerable.GetType() == typeof(List<KeyValuePair<TKey, TValue>>))
{
span = CollectionsMarshal.AsSpan((List<KeyValuePair<TKey, TValue>>)enumerable);
}
else
{
// Fallback path for all other enumerables
foreach (KeyValuePair<TKey, TValue> pair in enumerable)
{
Add(pair.Key, pair.Value);
}
return;
}

// We got a span. Add the elements to the dictionary.
foreach (KeyValuePair<TKey, TValue> pair in span)
{
Add(pair.Key, pair.Value);
}
Expand Down

0 comments on commit 709444f

Please sign in to comment.