-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Special-case array/list/ReadOnlyDictionary in Dictionary(IEnumerable, …) ctors #86254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsIt's reasonably common to construct a dictionary from a
private KeyValuePair<int, int>[] _array;
private List<KeyValuePair<int, int>> _list;
private IEnumerable<KeyValuePair<int, int>> _other;
private ReadOnlyDictionary<int, int> _rod;
[Params(1, 10, 100, 1000)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{
_array = Enumerable.Range(0, Length).Select(i => new KeyValuePair<int, int>(i, i)).ToArray();
_list = new List<KeyValuePair<int, int>>(_array);
_other = _array.Select(kvp => kvp);
_rod = new ReadOnlyDictionary<int, int>(_array.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
}
[Benchmark] public Dictionary<int, int> FromArray() => new Dictionary<int, int>(_array);
[Benchmark] public Dictionary<int, int> FromList() => new Dictionary<int, int>(_list);
[Benchmark] public Dictionary<int, int> FromOther() => new Dictionary<int, int>(_other);
[Benchmark] public Dictionary<int, int> FromReadOnlyDictionary() => new Dictionary<int, int>(_rod);
|
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs
Outdated
Show resolved
Hide resolved
… ...) 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. We can also unwrap a Dictionary wrapped in a ReadOnlyDictionary in order to better utliize the existing optimizations around Dictionary.
185d62f
to
8ed9267
Compare
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs
Outdated
Show resolved
Hide resolved
…jectModel/ReadOnlyDictionary.cs
It's reasonably common to construct a dictionary from a
KeyValuePair<TKey, TValue>[]
or aList<KeyValuePair<TKey, TValue>>
, and we can make both cheaper. We can also unwrap a Dictionary wrapped in a ReadOnlyDictionary in order to better utliize the existing optimizations around Dictionary.