Skip to content
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

Add benchmarks for OrderedDictionary<,> #4557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/benchmarks/micro/MicroBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@
<Compile Remove="libraries\System.Numerics.Tensors\*.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &lt; '9.0')">
<Compile Remove="libraries\System.Collections\OrderedDictionary\*.cs" />
</ItemGroup>

<!-- This is not removing things from older Net versions, it is removing from newer Net versions -->
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &gt;= '8.0')">
<Compile Remove="libraries\System.Drawing\*.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,17 @@ public ObservableCollection<T> ObservableCollection()
}
return collection;
}

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
var collection = new OrderedDictionary<T, T>(Size);
var uniqueValues = _uniqueValues;
for (int i = 0; i < uniqueValues.Length; i++)
collection.Add(uniqueValues[i], uniqueValues[i]);
return collection;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
using System.Drawing;

namespace System.Collections
{
Expand Down Expand Up @@ -44,5 +45,17 @@ public ConcurrentDictionary<T, T> ConcurrentDictionary()
collection.TryAdd(uniqueValues[i], uniqueValues[i]);
return collection;
}

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
var collection = new OrderedDictionary<T, T>();
var uniqueValues = _uniqueValues;
for (int i = 0; i < uniqueValues.Length; i++)
collection.TryAdd(uniqueValues[i], uniqueValues[i]);
return collection;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,17 @@ public ConcurrentDictionary<T, T> ConcurrentDictionary()
collection.TryAdd(uniqueValues[i], uniqueValues[i]);
return collection;
}

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
var collection = new OrderedDictionary<T, T>(Count);
var uniqueValues = _uniqueValues;
for (int i = 0; i < uniqueValues.Length; i++)
collection.TryAdd(uniqueValues[i], uniqueValues[i]);
return collection;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class ContainsKeyFalse<TKey, TValue>
private ImmutableDictionary<TKey, TValue> _immutableDictionary;
private ImmutableSortedDictionary<TKey, TValue> _immutableSortedDictionary;
private FrozenDictionary<TKey, TValue> _frozenDictionary;
#if NET9_0_OR_GREATER
private OrderedDictionary<TKey, TValue> _orderedDictionary;
#endif

[Params(Utils.DefaultCollectionSize)]
public int Size;
Expand All @@ -47,6 +50,9 @@ public void Setup()
_immutableDictionary = Immutable.ImmutableDictionary.CreateRange<TKey, TValue>(_source);
_immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange<TKey, TValue>(_source);
_frozenDictionary = _source.ToFrozenDictionary();
#if NET9_0_OR_GREATER
_orderedDictionary = new OrderedDictionary<TKey, TValue>(_source);
#endif
}

[Benchmark]
Expand Down Expand Up @@ -139,5 +145,18 @@ public bool FrozenDictionary()
result ^= collection.ContainsKey(notFound[i]);
return result;
}

#if NET9_0_OR_GREATER
[Benchmark]
public bool OrderedDictionary()
{
bool result = default;
var collection = _orderedDictionary;
var notFound = _notFound;
for (int i = 0; i < notFound.Length; i++)
result ^= collection.ContainsKey(notFound[i]);
return result;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class ContainsKeyTrue<TKey, TValue>
private ImmutableDictionary<TKey, TValue> _immutableDictionary;
private ImmutableSortedDictionary<TKey, TValue> _immutableSortedDictionary;
private FrozenDictionary<TKey, TValue> _frozenDictionary;
#if NET9_0_OR_GREATER
private OrderedDictionary<TKey, TValue> _orderedDictionary;
#endif

[Params(Utils.DefaultCollectionSize)]
public int Size;
Expand All @@ -45,6 +48,9 @@ public void Setup()
_immutableDictionary = Immutable.ImmutableDictionary.CreateRange<TKey, TValue>(_source);
_immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange<TKey, TValue>(_source);
_frozenDictionary = _source.ToFrozenDictionary();
#if NET9_0_OR_GREATER
_orderedDictionary = new OrderedDictionary<TKey, TValue>(_source);
#endif
}

[Benchmark]
Expand Down Expand Up @@ -137,5 +143,18 @@ public bool FrozenDictionary()
result ^= collection.ContainsKey(found[i]);
return result;
}

#if NET9_0_OR_GREATER
[Benchmark]
public bool OrderedDictionary()
{
bool result = default;
var collection = _orderedDictionary;
var found = _found;
for (int i = 0; i < found.Length; i++)
result ^= collection.ContainsKey(found[i]);
return result;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ public class CtorDefaultSize<T>

[Benchmark]
public ConcurrentBag<T> ConcurrentBag() => new ConcurrentBag<T>();

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary() => new OrderedDictionary<T, T>();
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public class CtorFromCollection<T>
nameof(FrozenSet)})]
public void SetupCollection() => _collection = ValuesGenerator.ArrayOfUniqueValues<T>(Size);

[GlobalSetup(Targets = new[] { nameof(Dictionary), nameof(SortedList), nameof(SortedDictionary), nameof(ConcurrentDictionary), nameof(ImmutableDictionary), nameof(ImmutableSortedDictionary), nameof(FrozenDictionaryOptimized) })]
[GlobalSetup(Targets = new[] { nameof(Dictionary), nameof(SortedList), nameof(SortedDictionary), nameof(ConcurrentDictionary), nameof(ImmutableDictionary), nameof(ImmutableSortedDictionary), nameof(FrozenDictionaryOptimized),
#if NET9_0_OR_GREATER
nameof(OrderedDictionary)
#endif
})]
public void SetupDictionary() => _dictionary = ValuesGenerator.Dictionary<T, T>(Size);

[GlobalSetup(Targets = new[] { nameof(SortedDictionaryDeepCopy) })]
Expand Down Expand Up @@ -107,5 +111,10 @@ public FrozenDictionary<T, T> FrozenDictionaryOptimized() // we kept the old nam

[Benchmark]
public FrozenSet<T> FrozenSet() => _collection.ToFrozenSet();

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary() => new OrderedDictionary<T, T>(_dictionary);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public class CtorGivenSize<T>

[Benchmark]
public ConcurrentDictionary<T, T> ConcurrentDictionary() => new ConcurrentDictionary<T, T>(Utils.ConcurrencyLevel, Size);

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary() => new OrderedDictionary<T, T>(Size);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,19 @@ public ImmutableQueue<T> ImmutableQueue()
}
return immutableQueue.Clear();
}

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
OrderedDictionary<T, T> orderedDictionary = new OrderedDictionary<T, T>();
foreach (T value in _uniqueValues)
{
orderedDictionary.Add(value, value);
}
orderedDictionary.Clear();
return orderedDictionary;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,22 @@ public Queue<T> Queue()
}
return queue;
}

#if NET9_0_OR_GREATER
[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
OrderedDictionary<T, T> orderedDictionary = new OrderedDictionary<T, T>();
foreach (T key in _keys)
{
orderedDictionary.Add(key, key);
}
foreach (T key in _keys)
{
orderedDictionary.Remove(key);
}
return orderedDictionary;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class IndexerSet<T>
private SortedList<T, T> _sortedList;
private SortedDictionary<T, T> _sortedDictionary;
private ConcurrentDictionary<T, T> _concurrentDictionary;
#if NET9_0_OR_GREATER
private OrderedDictionary<T, T> _orderedDictionary;
#endif

[GlobalSetup(Targets = new[] { nameof(Array), nameof(Span) })]
public void SetupArray() => _array = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
Expand Down Expand Up @@ -143,5 +146,34 @@ public ConcurrentDictionary<T, T> ConcurrentDictionary()
dictionary[keys[i]] = default;
return dictionary;
}

#if NET9_0_OR_GREATER
[GlobalSetup(Targets = [ nameof(OrderedDictionary), nameof(OrderedDictionary_SetAt) ])]
public void SetupOrderedDictionary()
{
_keys = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
_orderedDictionary = new OrderedDictionary<T, T>(_keys.ToDictionary(i => i, i => i));
}

[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary()
{
var dictionary = _orderedDictionary;
var keys = _keys;
for (int i = 0; i < keys.Length; i++)
dictionary[keys[i]] = default;
return dictionary;
}

[Benchmark]
public OrderedDictionary<T, T> OrderedDictionary_SetAt()
{
var dictionary = _orderedDictionary;
var keys = _keys;
for (int i = 0; i < keys.Length; i++)
dictionary.SetAt(i, keys[i], default);
return dictionary;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class IterateFor<T>
private ImmutableArray<T> _immutablearray;
private ImmutableList<T> _immutablelist;
private ImmutableSortedSet<T> _immutablesortedset;
#if NET9_0_OR_GREATER
private OrderedDictionary<T, T> _ordereddictionary;
#endif


[GlobalSetup(Target = nameof(Array))]
public void SetupArray() => _array = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
Expand Down Expand Up @@ -134,5 +138,20 @@ public T ImmutableSortedSet()
result = collection[i];
return result;
}

#if NET9_0_OR_GREATER
[GlobalSetup(Target = nameof(OrderedDictionary))]
public void SetupOrderedDictionary() => _ordereddictionary = new OrderedDictionary<T, T>(ValuesGenerator.Dictionary<T, T>(Size));

[Benchmark]
public KeyValuePair<T, T> OrderedDictionary()
{
KeyValuePair<T, T> result = default;
var collection = _ordereddictionary;
for (int i = 0; i < collection.Count; i++)
result = collection.GetAt(i);
return result;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class IterateForEach<T>
private ImmutableSortedSet<T> _immutablesortedset;
private FrozenDictionary<T, T> _frozenDictionary;
private FrozenSet<T> _frozenset;
#if NET9_0_OR_GREATER
private OrderedDictionary<T, T> _orderedDictionary;
#endif

[GlobalSetup(Targets = new [] { nameof(Array), nameof(Span), nameof(ReadOnlySpan)})]
public void SetupArray() => _array = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
Expand Down Expand Up @@ -396,5 +399,20 @@ public T FrozenSet()
result = item;
return result;
}

#if NET9_0_OR_GREATER
[GlobalSetup(Target = nameof(OrderedDictionary))]
public void SetupOrderedDictionary() => _orderedDictionary = new OrderedDictionary<T, T>(ValuesGenerator.Dictionary<T, T>(Size));

[Benchmark]
public T OrderedDictionary()
{
T result = default;
var collection = _orderedDictionary;
foreach (var item in collection)
result = item.Value;
return result;
}
#endif
}
}
Loading
Loading