Skip to content

Commit

Permalink
SortedDictionary Copy optimization (#45659)
Browse files Browse the repository at this point in the history
* Sorted Dictionary Copy optimization

* Update src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

* Feedback

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
johnthcall and jkotas authored Dec 14, 2020
1 parent 8d6e098 commit b01c8e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ public SortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey>? c
throw new ArgumentNullException(nameof(dictionary));
}

_set = new TreeSet<KeyValuePair<TKey, TValue>>(new KeyValuePairComparer(comparer));
var keyValuePairComparer = new KeyValuePairComparer(comparer);

foreach (KeyValuePair<TKey, TValue> pair in dictionary)
if (dictionary is SortedDictionary<TKey, TValue> sortedDictionary &&
sortedDictionary._set.Comparer is KeyValuePairComparer kv &&
kv.keyComparer.Equals(keyValuePairComparer.keyComparer))
{
_set = new TreeSet<KeyValuePair<TKey, TValue>>(sortedDictionary._set, keyValuePairComparer);
}
else
{
_set.Add(pair);
_set = new TreeSet<KeyValuePair<TKey, TValue>>(keyValuePairComparer);

foreach (KeyValuePair<TKey, TValue> pair in dictionary)
{
_set.Add(pair);
}
}
}

Expand Down Expand Up @@ -961,6 +972,8 @@ public TreeSet()

public TreeSet(IComparer<T>? comparer) : base(comparer) { }

internal TreeSet(TreeSet<T> set, IComparer<T>? comparer) : base(set, comparer) { }

private TreeSet(SerializationInfo siInfo, StreamingContext context) : base(siInfo, context) { }

internal override bool AddIfNotPresent(T item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,18 @@ public void SortedDictionary_Generic_Constructor_IDictionary_IComparer(int count
{
IComparer<TKey> comparer = GetKeyIComparer();
IDictionary<TKey, TValue> source = GenericIDictionaryFactory(count);
SortedDictionary<TKey, TValue> copied = new SortedDictionary<TKey, TValue>(source, comparer);
Assert.Equal(source, copied);
SortedDictionary<TKey, TValue> sourceSorted = new SortedDictionary<TKey, TValue>(source, comparer);
Assert.Equal(source, sourceSorted);
Assert.Equal(comparer, sourceSorted.Comparer);
// Test copying a sorted dictionary.
SortedDictionary<TKey, TValue> copied = new SortedDictionary<TKey, TValue>(sourceSorted, comparer);
Assert.Equal(sourceSorted, copied);
Assert.Equal(comparer, copied.Comparer);
// Test copying a sorted dictionary with a different comparer.
IComparer<TKey> reverseComparer = Comparer<TKey>.Create((key1, key2) => -comparer.Compare(key1, key2));
SortedDictionary<TKey, TValue> copiedReverse = new SortedDictionary<TKey, TValue>(sourceSorted, reverseComparer);
Assert.Equal(sourceSorted, copiedReverse);
Assert.Equal(reverseComparer, copiedReverse.Comparer);
}

#endregion
Expand Down

0 comments on commit b01c8e1

Please sign in to comment.