From f5ab03944b044e56d99fc0684aec90a2da31a3b3 Mon Sep 17 00:00:00 2001 From: Michael Adelson Date: Fri, 18 Feb 2022 18:31:46 -0500 Subject: [PATCH 1/6] Add some additional micro benchmark coverage for immutable collections. See https://github.com/dotnet/runtime/issues/14477#issuecomment-1037693750 for context. --- .../System.Collections/Add/AddGivenSize.cs | 67 +++++++++++++++++++ .../AddRemove/AddRemoveSteadyState.cs | 22 ++++++ .../Dictionary/DictionarySequentialKeys.cs | 45 +++++++++++++ 3 files changed, 134 insertions(+) diff --git a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs index 4e24ede54ab..347d628e996 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs @@ -4,6 +4,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.Collections.ObjectModel; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; @@ -133,5 +134,71 @@ public ObservableCollection ObservableCollection() } return collection; } + + [Benchmark] + public ImmutableArray ImmutableArray() + { + var collection = ImmutableArray.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value); + } + return collection; + } + + [Benchmark] + public ImmutableDictionary ImmutableDictionary() + { + var collection = ImmutableDictionary.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value, value); + } + return collection; + } + + [Benchmark] + public ImmutableHashSet ImmutableHashSet() + { + var collection = ImmutableHashSet.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value); + } + return collection; + } + + [Benchmark] + public ImmutableList ImmutableList() + { + var collection = ImmutableList.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value); + } + return collection; + } + + [Benchmark] + public ImmutableSortedDictionary ImmutableSortedDictionary() + { + var collection = ImmutableSortedDictionary.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value, value); + } + return collection; + } + + [Benchmark] + public ImmutableSortedSet ImmutableSortedSet() + { + var collection = ImmutableSortedSet.Empty; + foreach (T value in _uniqueValues) + { + collection = collection.Add(value); + } + return collection; + } } } \ No newline at end of file diff --git a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs index d16fd9cdbe0..0c5a6e2601b 100644 --- a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs +++ b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs @@ -4,6 +4,8 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Extensions; using MicroBenchmarks; @@ -20,6 +22,8 @@ public class Add_Remove_SteadyState // serialized producer/consumer throughpu private ConcurrentStack _concurrentStack; private Queue _queue; private Stack _stack; + private ImmutableQueue _immutableQueue; + private ImmutableStack _immutableStack; [Params(Utils.DefaultCollectionSize)] public int Count; @@ -73,5 +77,23 @@ public void Stack() T item = _stack.Pop(); _stack.Push(item); } + + [GlobalSetup(Target = nameof(Queue))] + public void SetupImmutableQueue() => _immutableQueue = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableQueue.Empty, (q, v) => q.Enqueue(v)); + + [Benchmark] + public void ImmutableQueue() + { + _immutableQueue = _immutableQueue.Dequeue(out T item).Enqueue(item); + } + + [GlobalSetup(Target = nameof(Stack))] + public void SetupImmutableStack() => _immutableStack = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableStack.Empty, (q, v) => q.Push(v)); + + [Benchmark] + public void ImmutableStack() + { + _immutableStack = _immutableStack.Pop(out T item).Push(item); + } } } diff --git a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs index 719882d8e0f..52e74e64e1e 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using BenchmarkDotNet.Attributes; using MicroBenchmarks; @@ -16,12 +17,34 @@ public class DictionarySequentialKeys private const int ThreeThousand = 3_000; private Dictionary _dict_3k; + private SortedDictionary _sortedDict_3k; + private ImmutableDictionary _immutableDict_3k; + private ImmutableSortedDictionary _immutableSortedDict_3k; + private Dictionary _dict32ByteValue_3k; + private SortedDictionary _sortedDict32ByteValue_3k; + private ImmutableDictionary _immutableDict32ByteValue_3k; + private ImmutableSortedDictionary _immutableSortedDict32ByteValue_3k; + private Dictionary _dict32ByteRefsValue_3k; + private SortedDictionary _sortedDict32ByteRefsValue_3k; + private ImmutableDictionary _immutableDict32ByteRefsValue_3k; + private ImmutableSortedDictionary _immutableSortedDict32ByteRefsValue_3k; private Dictionary _dict_17; + private SortedDictionary _sortedDict_17; + private ImmutableDictionary _immutableDict_17; + private ImmutableSortedDictionary _immutableSortedDict_17; + private Dictionary _dict32ByteValue_17; + private SortedDictionary _sortedDict32ByteValue_17; + private ImmutableDictionary _immutableDict32ByteValue_17; + private ImmutableSortedDictionary _immutableSortedDict32ByteValue_17; + private Dictionary _dict32ByteRefsValue_17; + private SortedDictionary _sortedDict32ByteRefsValue_17; + private ImmutableDictionary _immutableDict32ByteRefsValue_17; + private ImmutableSortedDictionary _immutableSortedDict32ByteRefsValue_17; [GlobalSetup] public void Initialize() @@ -29,12 +52,34 @@ public void Initialize() (object, object, object, object) item = (new object(), new object(), new object(), new object()); _dict_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i); + _sortedDict_17 = new(_dict_17); + _immutableDict_17 = _dict_17.ToImmutableDictionary(); + _immutableSortedDict_17 = _dict_17.ToImmutableSortedDictionary(); + _dict32ByteValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => default((long, long, long, long))); + _sortedDict32ByteValue_17 = new(_dict32ByteValue_17); + _immutableDict32ByteValue_17 = _dict32ByteValue_17.ToImmutableDictionary(); + _immutableSortedDict32ByteValue_17 = _dict32ByteValue_17.ToImmutableSortedDictionary(); + _dict32ByteRefsValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => item); + _sortedDict32ByteRefsValue_17 = new(_dict32ByteRefsValue_17); + _immutableDict32ByteRefsValue_17 = _dict32ByteRefsValue_17.ToImmutableDictionary(); + _immutableSortedDict32ByteRefsValue_17 = _dict32ByteRefsValue_17.ToImmutableSortedDictionary(); _dict_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i); + _sortedDict_3k = new(_dict_3k); + _immutableDict_3k = _dict_3k.ToImmutableDictionary(); + _immutableSortedDict_3k = _dict_3k.ToImmutableSortedDictionary(); + _dict32ByteValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => default((long, long, long, long))); + _sortedDict32ByteValue_3k = new(_dict32ByteValue_3k); + _immutableDict32ByteValue_3k = _dict32ByteValue_3k.ToImmutableDictionary(); + _immutableSortedDict32ByteValue_3k = _dict32ByteValue_3k.ToImmutableSortedDictionary(); + _dict32ByteRefsValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => item); + _sortedDict32ByteRefsValue_3k = new(_dict32ByteRefsValue_3k); + _immutableDict32ByteRefsValue_3k = _dict32ByteRefsValue_3k.ToImmutableDictionary(); + _immutableSortedDict32ByteRefsValue_3k = _dict32ByteRefsValue_3k.ToImmutableSortedDictionary(); } [Benchmark(OperationsPerInvoke = Seventeen)] From 6af548d5912949e9d3c24657a1dbc117b587188f Mon Sep 17 00:00:00 2001 From: Michael Adelson Date: Fri, 18 Feb 2022 20:36:28 -0500 Subject: [PATCH 2/6] Add some additional micro benchmark coverage for immutable collections --- .../Dictionary/DictionarySequentialKeys.cs | 418 +++++++++++++++++- 1 file changed, 416 insertions(+), 2 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs index 52e74e64e1e..b69d9eaa1c5 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs @@ -86,26 +86,98 @@ public void Initialize() public int ContainsValue_17_Int_Int() => ContainsKey_Int_Int(_dict_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsValue_17_Int_Int_Sorted() + => ContainsKey_Int_Int(_sortedDict_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsValue_17_Int_Int_Immutable() + => ContainsKey_Int_Int(_immutableDict_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsValue_17_Int_Int_ImmutableSorted() + => ContainsKey_Int_Int(_immutableSortedDict_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] public int ContainsKey_17_Int_32ByteValue() => ContainsKey_Int_LargeStruct(_dict32ByteValue_17, Seventeen); [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteRefsValue() + public int ContainsKey_17_Int_32ByteValue_Sorted() + => ContainsKey_Int_LargeStruct(_sortedDict32ByteValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteValue_Immutable() + => ContainsKey_Int_LargeStruct(_immutableDict32ByteValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteValue_ImmutableSorted() + => ContainsKey_Int_LargeStruct(_immutableSortedDict32ByteValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteRefsValue() => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteRefsValue_Sorted() + => ContainsKey_Int_LargeRefStruct(_sortedDict32ByteRefsValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteRefsValue_Immutable() + => ContainsKey_Int_LargeRefStruct(_immutableDict32ByteRefsValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int ContainsKey_17_Int_32ByteRefsValue_ImmutableSorted() + => ContainsKey_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsValue_3k_Int_Int() + public int ContainsValue_3k_Int_Int() => ContainsKey_Int_Int(_dict_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsValue_3k_Int_Int_Sorted() + => ContainsKey_Int_Int(_sortedDict_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsValue_3k_Int_Int_Immutable() + => ContainsKey_Int_Int(_immutableDict_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsValue_3k_Int_Int_ImmutableSorted() + => ContainsKey_Int_Int(_immutableSortedDict_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] public int ContainsKey_3k_Int_32ByteValue() => ContainsKey_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteValue_Sorted() + => ContainsKey_Int_LargeStruct(_sortedDict32ByteValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteValue_Immutable() + => ContainsKey_Int_LargeStruct(_immutableDict32ByteValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteValue_ImmutableSorted() + => ContainsKey_Int_LargeStruct(_immutableSortedDict32ByteValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] public int ContainsKey_3k_Int_32ByteRefsValue() => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteRefsValue_Sorted() + => ContainsKey_Int_LargeRefStruct(_sortedDict32ByteRefsValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteRefsValue_Immutable() + => ContainsKey_Int_LargeRefStruct(_immutableDict32ByteRefsValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int ContainsKey_3k_Int_32ByteRefsValue_ImmutableSorted() + => ContainsKey_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_3k, ThreeThousand); + private static int ContainsKey_Int_Int(Dictionary d, int count) { int total = 0; @@ -121,6 +193,51 @@ private static int ContainsKey_Int_Int(Dictionary d, int count) return total; } + private static int ContainsKey_Int_Int(SortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_Int(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_Int(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + private static int ContainsKey_Int_LargeStruct(Dictionary d, int count) { int total = 0; @@ -136,6 +253,51 @@ private static int ContainsKey_Int_LargeStruct(Dictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_LargeStruct(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_LargeStruct(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + private static int ContainsKey_Int_LargeRefStruct(Dictionary d, int count) { int total = 0; @@ -151,30 +313,147 @@ private static int ContainsKey_Int_LargeRefStruct(Dictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_LargeRefStruct(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + + private static int ContainsKey_Int_LargeRefStruct(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.ContainsKey(i)) + { + total++; + } + } + + return total; + } + [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_Int() => TryGetValue_Int_Int(_dict_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_Int_Sorted() + => TryGetValue_Int_Int(_sortedDict_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_Int_Immutable() + => TryGetValue_Int_Int(_immutableDict_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_Int_ImmutableSorted() + => TryGetValue_Int_Int(_immutableSortedDict_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_32ByteValue() => TryGetValue_Int_LargeStruct(_dict32ByteValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteValue_Sorted() + => TryGetValue_Int_LargeStruct(_sortedDict32ByteValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteValue_Immutable() + => TryGetValue_Int_LargeStruct(_immutableDict32ByteValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteValue_ImmutableSorted() + => TryGetValue_Int_LargeStruct(_immutableSortedDict32ByteValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_32ByteRefsValue() => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteRefsValue_Sorted() + => TryGetValue_Int_LargeRefStruct(_sortedDict32ByteRefsValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteRefsValue_Immutable() + => TryGetValue_Int_LargeRefStruct(_immutableDict32ByteRefsValue_17, Seventeen); + + [Benchmark(OperationsPerInvoke = Seventeen)] + public int TryGetValue_17_Int_32ByteRefsValue_ImmutableSorted() + => TryGetValue_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_17, Seventeen); + [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_Int() => TryGetValue_Int_Int(_dict_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_Int_Sorted() + => TryGetValue_Int_Int(_sortedDict_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_Int_Immutable() + => TryGetValue_Int_Int(_immutableDict_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_Int_ImmutableSorted() + => TryGetValue_Int_Int(_immutableSortedDict_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_32ByteValue() => TryGetValue_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteValue_Sorted() + => TryGetValue_Int_LargeStruct(_sortedDict32ByteValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteValue_Immutable() + => TryGetValue_Int_LargeStruct(_immutableDict32ByteValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteValue_ImmutableSorted() + => TryGetValue_Int_LargeStruct(_immutableSortedDict32ByteValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_32ByteRefsValue() => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteRefsValue_Sorted() + => TryGetValue_Int_LargeRefStruct(_sortedDict32ByteRefsValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteRefsValue_Immutable() + => TryGetValue_Int_LargeRefStruct(_immutableDict32ByteRefsValue_3k, ThreeThousand); + + [Benchmark(OperationsPerInvoke = ThreeThousand)] + public int TryGetValue_3k_Int_32ByteRefsValue_ImmutableSorted() + => TryGetValue_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_3k, ThreeThousand); + private static int TryGetValue_Int_Int(Dictionary d, int count) { int total = 0; @@ -190,6 +469,51 @@ private static int TryGetValue_Int_Int(Dictionary d, int count) return total; } + private static int TryGetValue_Int_Int(SortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_Int(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_Int(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + private static int TryGetValue_Int_LargeStruct(Dictionary d, int count) { int total = 0; @@ -205,6 +529,51 @@ private static int TryGetValue_Int_LargeStruct(Dictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_LargeStruct(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_LargeStruct(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + private static int TryGetValue_Int_LargeRefStruct(Dictionary d, int count) { int total = 0; @@ -219,5 +588,50 @@ private static int TryGetValue_Int_LargeRefStruct(Dictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_LargeRefStruct(ImmutableDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } + + private static int TryGetValue_Int_LargeRefStruct(ImmutableSortedDictionary d, int count) + { + int total = 0; + + for (int i = 0; i < count; i++) + { + if (d.TryGetValue(i, out _)) + { + total++; + } + } + + return total; + } } } From 028bcd52c1e0d0f3f3facb7364be54abddd1ce25 Mon Sep 17 00:00:00 2001 From: Michael Adelson Date: Sat, 5 Mar 2022 14:30:12 -0500 Subject: [PATCH 3/6] Fix GlobalSetup Targets for AddRemoveSteadyState --- .../System.Collections/AddRemove/AddRemoveSteadyState.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs index 0c5a6e2601b..c12ff802326 100644 --- a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs +++ b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs @@ -78,7 +78,7 @@ public void Stack() _stack.Push(item); } - [GlobalSetup(Target = nameof(Queue))] + [GlobalSetup(Target = nameof(ImmutableQueue))] public void SetupImmutableQueue() => _immutableQueue = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableQueue.Empty, (q, v) => q.Enqueue(v)); [Benchmark] @@ -87,7 +87,7 @@ public void ImmutableQueue() _immutableQueue = _immutableQueue.Dequeue(out T item).Enqueue(item); } - [GlobalSetup(Target = nameof(Stack))] + [GlobalSetup(Target = nameof(ImmutableStack))] public void SetupImmutableStack() => _immutableStack = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableStack.Empty, (q, v) => q.Push(v)); [Benchmark] From bbb46113b8aa68540ce7ff58d80c1b41eb4d19cb Mon Sep 17 00:00:00 2001 From: madelson <1269046+madelson@users.noreply.github.com> Date: Mon, 7 Mar 2022 18:10:16 -0500 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Adam Sitnik --- .../System.Collections/AddRemove/AddRemoveSteadyState.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs index c12ff802326..791e0d30394 100644 --- a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs +++ b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs @@ -5,7 +5,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Extensions; using MicroBenchmarks; @@ -79,7 +78,7 @@ public void Stack() } [GlobalSetup(Target = nameof(ImmutableQueue))] - public void SetupImmutableQueue() => _immutableQueue = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableQueue.Empty, (q, v) => q.Enqueue(v)); + public void SetupImmutableQueue() => _immutableQueue = Immutable.ImmutableQueue.CreateRange(ValuesGenerator.ArrayOfUniqueValues(Count)); [Benchmark] public void ImmutableQueue() @@ -88,7 +87,7 @@ public void ImmutableQueue() } [GlobalSetup(Target = nameof(ImmutableStack))] - public void SetupImmutableStack() => _immutableStack = ValuesGenerator.ArrayOfUniqueValues(Count).Aggregate(ImmutableStack.Empty, (q, v) => q.Push(v)); + public void SetupImmutableStack() => _immutableStack = Immutable.ImmutableStack.CreateRange(ValuesGenerator.ArrayOfUniqueValues(Count)); [Benchmark] public void ImmutableStack() From 5fbc657649852209bb193160707fc0635a0a23e1 Mon Sep 17 00:00:00 2001 From: Michael Adelson Date: Mon, 7 Mar 2022 18:38:04 -0500 Subject: [PATCH 5/6] Addres feedback from https://github.com/dotnet/performance/pull/2268 --- .../System.Collections/Add/AddGivenSize.cs | 66 --- .../System.Collections/CreateAddAndClear.cs | 89 ++++ .../Dictionary/DictionarySequentialKeys.cs | 463 +----------------- 3 files changed, 91 insertions(+), 527 deletions(-) diff --git a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs index 347d628e996..9638ebcefcb 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs @@ -134,71 +134,5 @@ public ObservableCollection ObservableCollection() } return collection; } - - [Benchmark] - public ImmutableArray ImmutableArray() - { - var collection = ImmutableArray.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value); - } - return collection; - } - - [Benchmark] - public ImmutableDictionary ImmutableDictionary() - { - var collection = ImmutableDictionary.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value, value); - } - return collection; - } - - [Benchmark] - public ImmutableHashSet ImmutableHashSet() - { - var collection = ImmutableHashSet.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value); - } - return collection; - } - - [Benchmark] - public ImmutableList ImmutableList() - { - var collection = ImmutableList.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value); - } - return collection; - } - - [Benchmark] - public ImmutableSortedDictionary ImmutableSortedDictionary() - { - var collection = ImmutableSortedDictionary.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value, value); - } - return collection; - } - - [Benchmark] - public ImmutableSortedSet ImmutableSortedSet() - { - var collection = ImmutableSortedSet.Empty; - foreach (T value in _uniqueValues) - { - collection = collection.Add(value); - } - return collection; - } } } \ No newline at end of file diff --git a/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs b/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs index 49fd2c43784..247be837355 100644 --- a/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs +++ b/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs @@ -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 @@ -237,5 +238,93 @@ public ConcurrentBag ConcurrentBag() return concurrentBag; } #endif + + [Benchmark] + public ImmutableArray ImmutableArray() + { + ImmutableArray immutableArray = ImmutableArray.Empty; + foreach (T value in _uniqueValues) + { + immutableArray = immutableArray.Add(value); + } + return immutableArray.Clear(); + } + + [Benchmark] + public ImmutableList ImmutableList() + { + ImmutableList immutableList = ImmutableList.Empty; + foreach (T value in _uniqueValues) + { + immutableList = immutableList.Add(value); + } + return immutableList.Clear(); + } + + [Benchmark] + public ImmutableDictionary ImmutableDictionary() + { + ImmutableDictionary immutableDictionary = ImmutableDictionary.Empty; + foreach (T value in _uniqueValues) + { + immutableDictionary = immutableDictionary.Add(value, value); + } + return immutableDictionary.Clear(); + } + + [Benchmark] + public ImmutableHashSet ImmutableHashSet() + { + ImmutableHashSet immutableHashSet = ImmutableHashSet.Empty; + foreach (T value in _uniqueValues) + { + immutableHashSet = immutableHashSet.Add(value); + } + return immutableHashSet.Clear(); + } + + [Benchmark] + public ImmutableSortedDictionary ImmutableSortedDictionary() + { + ImmutableSortedDictionary immutableSortedDictionary = ImmutableSortedDictionary.Empty; + foreach (T value in _uniqueValues) + { + immutableSortedDictionary = immutableSortedDictionary.Add(value, value); + } + return immutableSortedDictionary.Clear(); + } + + [Benchmark] + public ImmutableSortedSet ImmutableSortedSet() + { + ImmutableSortedSet immutableSortedSet = ImmutableSortedSet.Empty; + foreach (T value in _uniqueValues) + { + immutableSortedSet = immutableSortedSet.Add(value); + } + return immutableSortedSet.Clear(); + } + + [Benchmark] + public ImmutableStack ImmutableStack() + { + ImmutableStack immutableStack = ImmutableStack.Empty; + foreach (T value in _uniqueValues) + { + immutableStack = immutableStack.Push(value); + } + return immutableStack.Clear(); + } + + [Benchmark] + public ImmutableQueue ImmutableQueue() + { + ImmutableQueue immutableQueue = ImmutableQueue.Empty; + foreach (T value in _uniqueValues) + { + immutableQueue = immutableQueue.Enqueue(value); + } + return immutableQueue.Clear(); + } } } \ No newline at end of file diff --git a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs index b69d9eaa1c5..719882d8e0f 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Dictionary/DictionarySequentialKeys.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; using BenchmarkDotNet.Attributes; using MicroBenchmarks; @@ -17,34 +16,12 @@ public class DictionarySequentialKeys private const int ThreeThousand = 3_000; private Dictionary _dict_3k; - private SortedDictionary _sortedDict_3k; - private ImmutableDictionary _immutableDict_3k; - private ImmutableSortedDictionary _immutableSortedDict_3k; - private Dictionary _dict32ByteValue_3k; - private SortedDictionary _sortedDict32ByteValue_3k; - private ImmutableDictionary _immutableDict32ByteValue_3k; - private ImmutableSortedDictionary _immutableSortedDict32ByteValue_3k; - private Dictionary _dict32ByteRefsValue_3k; - private SortedDictionary _sortedDict32ByteRefsValue_3k; - private ImmutableDictionary _immutableDict32ByteRefsValue_3k; - private ImmutableSortedDictionary _immutableSortedDict32ByteRefsValue_3k; private Dictionary _dict_17; - private SortedDictionary _sortedDict_17; - private ImmutableDictionary _immutableDict_17; - private ImmutableSortedDictionary _immutableSortedDict_17; - private Dictionary _dict32ByteValue_17; - private SortedDictionary _sortedDict32ByteValue_17; - private ImmutableDictionary _immutableDict32ByteValue_17; - private ImmutableSortedDictionary _immutableSortedDict32ByteValue_17; - private Dictionary _dict32ByteRefsValue_17; - private SortedDictionary _sortedDict32ByteRefsValue_17; - private ImmutableDictionary _immutableDict32ByteRefsValue_17; - private ImmutableSortedDictionary _immutableSortedDict32ByteRefsValue_17; [GlobalSetup] public void Initialize() @@ -52,132 +29,38 @@ public void Initialize() (object, object, object, object) item = (new object(), new object(), new object(), new object()); _dict_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i); - _sortedDict_17 = new(_dict_17); - _immutableDict_17 = _dict_17.ToImmutableDictionary(); - _immutableSortedDict_17 = _dict_17.ToImmutableSortedDictionary(); - _dict32ByteValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => default((long, long, long, long))); - _sortedDict32ByteValue_17 = new(_dict32ByteValue_17); - _immutableDict32ByteValue_17 = _dict32ByteValue_17.ToImmutableDictionary(); - _immutableSortedDict32ByteValue_17 = _dict32ByteValue_17.ToImmutableSortedDictionary(); - _dict32ByteRefsValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => item); - _sortedDict32ByteRefsValue_17 = new(_dict32ByteRefsValue_17); - _immutableDict32ByteRefsValue_17 = _dict32ByteRefsValue_17.ToImmutableDictionary(); - _immutableSortedDict32ByteRefsValue_17 = _dict32ByteRefsValue_17.ToImmutableSortedDictionary(); _dict_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i); - _sortedDict_3k = new(_dict_3k); - _immutableDict_3k = _dict_3k.ToImmutableDictionary(); - _immutableSortedDict_3k = _dict_3k.ToImmutableSortedDictionary(); - _dict32ByteValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => default((long, long, long, long))); - _sortedDict32ByteValue_3k = new(_dict32ByteValue_3k); - _immutableDict32ByteValue_3k = _dict32ByteValue_3k.ToImmutableDictionary(); - _immutableSortedDict32ByteValue_3k = _dict32ByteValue_3k.ToImmutableSortedDictionary(); - _dict32ByteRefsValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => item); - _sortedDict32ByteRefsValue_3k = new(_dict32ByteRefsValue_3k); - _immutableDict32ByteRefsValue_3k = _dict32ByteRefsValue_3k.ToImmutableDictionary(); - _immutableSortedDict32ByteRefsValue_3k = _dict32ByteRefsValue_3k.ToImmutableSortedDictionary(); } [Benchmark(OperationsPerInvoke = Seventeen)] public int ContainsValue_17_Int_Int() => ContainsKey_Int_Int(_dict_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsValue_17_Int_Int_Sorted() - => ContainsKey_Int_Int(_sortedDict_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsValue_17_Int_Int_Immutable() - => ContainsKey_Int_Int(_immutableDict_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsValue_17_Int_Int_ImmutableSorted() - => ContainsKey_Int_Int(_immutableSortedDict_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] public int ContainsKey_17_Int_32ByteValue() => ContainsKey_Int_LargeStruct(_dict32ByteValue_17, Seventeen); [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteValue_Sorted() - => ContainsKey_Int_LargeStruct(_sortedDict32ByteValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteValue_Immutable() - => ContainsKey_Int_LargeStruct(_immutableDict32ByteValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteValue_ImmutableSorted() - => ContainsKey_Int_LargeStruct(_immutableSortedDict32ByteValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteRefsValue() + public int ContainsKey_17_Int_32ByteRefsValue() => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteRefsValue_Sorted() - => ContainsKey_Int_LargeRefStruct(_sortedDict32ByteRefsValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteRefsValue_Immutable() - => ContainsKey_Int_LargeRefStruct(_immutableDict32ByteRefsValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int ContainsKey_17_Int_32ByteRefsValue_ImmutableSorted() - => ContainsKey_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsValue_3k_Int_Int() + public int ContainsValue_3k_Int_Int() => ContainsKey_Int_Int(_dict_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsValue_3k_Int_Int_Sorted() - => ContainsKey_Int_Int(_sortedDict_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsValue_3k_Int_Int_Immutable() - => ContainsKey_Int_Int(_immutableDict_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsValue_3k_Int_Int_ImmutableSorted() - => ContainsKey_Int_Int(_immutableSortedDict_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] public int ContainsKey_3k_Int_32ByteValue() => ContainsKey_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteValue_Sorted() - => ContainsKey_Int_LargeStruct(_sortedDict32ByteValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteValue_Immutable() - => ContainsKey_Int_LargeStruct(_immutableDict32ByteValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteValue_ImmutableSorted() - => ContainsKey_Int_LargeStruct(_immutableSortedDict32ByteValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] public int ContainsKey_3k_Int_32ByteRefsValue() => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteRefsValue_Sorted() - => ContainsKey_Int_LargeRefStruct(_sortedDict32ByteRefsValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteRefsValue_Immutable() - => ContainsKey_Int_LargeRefStruct(_immutableDict32ByteRefsValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int ContainsKey_3k_Int_32ByteRefsValue_ImmutableSorted() - => ContainsKey_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_3k, ThreeThousand); - private static int ContainsKey_Int_Int(Dictionary d, int count) { int total = 0; @@ -193,51 +76,6 @@ private static int ContainsKey_Int_Int(Dictionary d, int count) return total; } - private static int ContainsKey_Int_Int(SortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_Int(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_Int(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - private static int ContainsKey_Int_LargeStruct(Dictionary d, int count) { int total = 0; @@ -253,51 +91,6 @@ private static int ContainsKey_Int_LargeStruct(Dictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_LargeStruct(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_LargeStruct(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - private static int ContainsKey_Int_LargeRefStruct(Dictionary d, int count) { int total = 0; @@ -313,147 +106,30 @@ private static int ContainsKey_Int_LargeRefStruct(Dictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_LargeRefStruct(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - - private static int ContainsKey_Int_LargeRefStruct(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.ContainsKey(i)) - { - total++; - } - } - - return total; - } - [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_Int() => TryGetValue_Int_Int(_dict_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_Int_Sorted() - => TryGetValue_Int_Int(_sortedDict_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_Int_Immutable() - => TryGetValue_Int_Int(_immutableDict_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_Int_ImmutableSorted() - => TryGetValue_Int_Int(_immutableSortedDict_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_32ByteValue() => TryGetValue_Int_LargeStruct(_dict32ByteValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteValue_Sorted() - => TryGetValue_Int_LargeStruct(_sortedDict32ByteValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteValue_Immutable() - => TryGetValue_Int_LargeStruct(_immutableDict32ByteValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteValue_ImmutableSorted() - => TryGetValue_Int_LargeStruct(_immutableSortedDict32ByteValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] public int TryGetValue_17_Int_32ByteRefsValue() => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteRefsValue_Sorted() - => TryGetValue_Int_LargeRefStruct(_sortedDict32ByteRefsValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteRefsValue_Immutable() - => TryGetValue_Int_LargeRefStruct(_immutableDict32ByteRefsValue_17, Seventeen); - - [Benchmark(OperationsPerInvoke = Seventeen)] - public int TryGetValue_17_Int_32ByteRefsValue_ImmutableSorted() - => TryGetValue_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_17, Seventeen); - [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_Int() => TryGetValue_Int_Int(_dict_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_Int_Sorted() - => TryGetValue_Int_Int(_sortedDict_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_Int_Immutable() - => TryGetValue_Int_Int(_immutableDict_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_Int_ImmutableSorted() - => TryGetValue_Int_Int(_immutableSortedDict_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_32ByteValue() => TryGetValue_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteValue_Sorted() - => TryGetValue_Int_LargeStruct(_sortedDict32ByteValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteValue_Immutable() - => TryGetValue_Int_LargeStruct(_immutableDict32ByteValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteValue_ImmutableSorted() - => TryGetValue_Int_LargeStruct(_immutableSortedDict32ByteValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] public int TryGetValue_3k_Int_32ByteRefsValue() => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteRefsValue_Sorted() - => TryGetValue_Int_LargeRefStruct(_sortedDict32ByteRefsValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteRefsValue_Immutable() - => TryGetValue_Int_LargeRefStruct(_immutableDict32ByteRefsValue_3k, ThreeThousand); - - [Benchmark(OperationsPerInvoke = ThreeThousand)] - public int TryGetValue_3k_Int_32ByteRefsValue_ImmutableSorted() - => TryGetValue_Int_LargeRefStruct(_immutableSortedDict32ByteRefsValue_3k, ThreeThousand); - private static int TryGetValue_Int_Int(Dictionary d, int count) { int total = 0; @@ -469,51 +145,6 @@ private static int TryGetValue_Int_Int(Dictionary d, int count) return total; } - private static int TryGetValue_Int_Int(SortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_Int(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_Int(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - private static int TryGetValue_Int_LargeStruct(Dictionary d, int count) { int total = 0; @@ -529,51 +160,6 @@ private static int TryGetValue_Int_LargeStruct(Dictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_LargeStruct(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_LargeStruct(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - private static int TryGetValue_Int_LargeRefStruct(Dictionary d, int count) { int total = 0; @@ -588,50 +174,5 @@ private static int TryGetValue_Int_LargeRefStruct(Dictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_LargeRefStruct(ImmutableDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } - - private static int TryGetValue_Int_LargeRefStruct(ImmutableSortedDictionary d, int count) - { - int total = 0; - - for (int i = 0; i < count; i++) - { - if (d.TryGetValue(i, out _)) - { - total++; - } - } - - return total; - } } } From 9805cadb17040fb622af42e365137b393be2e821 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Wed, 9 Mar 2022 11:30:01 +0100 Subject: [PATCH 6/6] Apply suggestions from code review --- .../micro/libraries/System.Collections/Add/AddGivenSize.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs index 9638ebcefcb..4e24ede54ab 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs +++ b/src/benchmarks/micro/libraries/System.Collections/Add/AddGivenSize.cs @@ -4,7 +4,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; -using System.Collections.Immutable; using System.Collections.ObjectModel; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes;