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 some additional micro benchmark coverage for immutable collections. #2268

Merged
Merged
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
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();
}
}
}