Skip to content

Commit

Permalink
Add some additional micro benchmark coverage for immutable collection…
Browse files Browse the repository at this point in the history
…s. (dotnet#2268)
  • Loading branch information
madelson authored Mar 9, 2022
1 parent 81b931a commit d2e6ba7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
Expand All @@ -20,6 +21,8 @@ public class Add_Remove_SteadyState<T> // serialized producer/consumer throughpu
private ConcurrentStack<T> _concurrentStack;
private Queue<T> _queue;
private Stack<T> _stack;
private ImmutableQueue<T> _immutableQueue;
private ImmutableStack<T> _immutableStack;

[Params(Utils.DefaultCollectionSize)]
public int Count;
Expand Down Expand Up @@ -73,5 +76,23 @@ public void Stack()
T item = _stack.Pop();
_stack.Push(item);
}

[GlobalSetup(Target = nameof(ImmutableQueue))]
public void SetupImmutableQueue() => _immutableQueue = Immutable.ImmutableQueue.CreateRange<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));

[Benchmark]
public void ImmutableQueue()
{
_immutableQueue = _immutableQueue.Dequeue(out T item).Enqueue(item);
}

[GlobalSetup(Target = nameof(ImmutableStack))]
public void SetupImmutableStack() => _immutableStack = Immutable.ImmutableStack.CreateRange<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));

[Benchmark]
public void ImmutableStack()
{
_immutableStack = _immutableStack.Pop(out T item).Push(item);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MicroBenchmarks;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace System.Collections
Expand Down Expand Up @@ -237,5 +238,93 @@ public ConcurrentBag<T> ConcurrentBag()
return concurrentBag;
}
#endif

[Benchmark]
public ImmutableArray<T> ImmutableArray()
{
ImmutableArray<T> immutableArray = ImmutableArray<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableArray = immutableArray.Add(value);
}
return immutableArray.Clear();
}

[Benchmark]
public ImmutableList<T> ImmutableList()
{
ImmutableList<T> immutableList = ImmutableList<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableList = immutableList.Add(value);
}
return immutableList.Clear();
}

[Benchmark]
public ImmutableDictionary<T, T> ImmutableDictionary()
{
ImmutableDictionary<T, T> immutableDictionary = ImmutableDictionary<T, T>.Empty;
foreach (T value in _uniqueValues)
{
immutableDictionary = immutableDictionary.Add(value, value);
}
return immutableDictionary.Clear();
}

[Benchmark]
public ImmutableHashSet<T> ImmutableHashSet()
{
ImmutableHashSet<T> immutableHashSet = ImmutableHashSet<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableHashSet = immutableHashSet.Add(value);
}
return immutableHashSet.Clear();
}

[Benchmark]
public ImmutableSortedDictionary<T, T> ImmutableSortedDictionary()
{
ImmutableSortedDictionary<T, T> immutableSortedDictionary = ImmutableSortedDictionary<T, T>.Empty;
foreach (T value in _uniqueValues)
{
immutableSortedDictionary = immutableSortedDictionary.Add(value, value);
}
return immutableSortedDictionary.Clear();
}

[Benchmark]
public ImmutableSortedSet<T> ImmutableSortedSet()
{
ImmutableSortedSet<T> immutableSortedSet = ImmutableSortedSet<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableSortedSet = immutableSortedSet.Add(value);
}
return immutableSortedSet.Clear();
}

[Benchmark]
public ImmutableStack<T> ImmutableStack()
{
ImmutableStack<T> immutableStack = ImmutableStack<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableStack = immutableStack.Push(value);
}
return immutableStack.Clear();
}

[Benchmark]
public ImmutableQueue<T> ImmutableQueue()
{
ImmutableQueue<T> immutableQueue = ImmutableQueue<T>.Empty;
foreach (T value in _uniqueValues)
{
immutableQueue = immutableQueue.Enqueue(value);
}
return immutableQueue.Clear();
}
}
}

0 comments on commit d2e6ba7

Please sign in to comment.