-
Notifications
You must be signed in to change notification settings - Fork 271
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
Runtime issue 14477 more immutable benchmarks #2306
Open
madelson
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
madelson:runtime-issue-14477-more-immutable-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93d0349
Add additional coverage to TryAddDefaultSize.
madelson a57ac93
Add ContainsValue benchmarks.
madelson 2be86b5
Remove now-redundant ContainsValue benchmark from Perf_Dictionary.
madelson 5dd5eae
Add SetItem benchmarks.
madelson 64770d7
Added Perf_ImmutableSortedSet benchmark.
madelson ea63c33
Add CopyTo benchmark for ImmutableList.
madelson ac3b84b
Add CreateAddAndRemove benchmarks for immutable collections.
madelson 6c25ca6
Add sort benchmarks for immutable collections.
madelson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/benchmarks/micro/libraries/System.Collections/Contains/ContainsValueFalse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Extensions; | ||
using MicroBenchmarks; | ||
|
||
namespace System.Collections | ||
{ | ||
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)] | ||
[GenericTypeArguments(typeof(int), typeof(int))] // value type | ||
[GenericTypeArguments(typeof(string), typeof(string))] // reference type | ||
public class ContainsValueFalse<TKey, TValue> | ||
{ | ||
private TValue[] _notFound; | ||
private Dictionary<TKey, TValue> _source; | ||
|
||
private Dictionary<TKey, TValue> _dictionary; | ||
private SortedList<TKey, TValue> _sortedList; | ||
private SortedDictionary<TKey, TValue> _sortedDictionary; | ||
private ImmutableDictionary<TKey, TValue> _immutableDictionary; | ||
private ImmutableSortedDictionary<TKey, TValue> _immutableSortedDictionary; | ||
|
||
[Params(Utils.DefaultCollectionSize)] | ||
public int Size; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
var values = ValuesGenerator.ArrayOfUniqueValues<TValue>(Size * 2); | ||
_notFound = values.Take(Size).ToArray(); | ||
|
||
_source = values.Skip(Size).Take(Size).ToDictionary(item => (TKey)(object)item, item => item); | ||
_dictionary = new Dictionary<TKey, TValue>(_source); | ||
_sortedList = new SortedList<TKey, TValue>(_source); | ||
_sortedDictionary = new SortedDictionary<TKey, TValue>(_source); | ||
_immutableDictionary = Immutable.ImmutableDictionary.CreateRange<TKey, TValue>(_source); | ||
_immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange<TKey, TValue>(_source); | ||
} | ||
|
||
[Benchmark] | ||
public bool Dictionary() | ||
{ | ||
bool result = default; | ||
var collection = _dictionary; | ||
var notFound = _notFound; | ||
for (int i = 0; i < notFound.Length; i++) | ||
result ^= collection.ContainsValue(notFound[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool SortedList() | ||
{ | ||
bool result = default; | ||
var collection = _sortedList; | ||
var notFound = _notFound; | ||
for (int i = 0; i < notFound.Length; i++) | ||
result ^= collection.ContainsValue(notFound[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool SortedDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _sortedDictionary; | ||
var notFound = _notFound; | ||
for (int i = 0; i < notFound.Length; i++) | ||
result ^= collection.ContainsValue(notFound[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool ImmutableDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _immutableDictionary; | ||
var notFound = _notFound; | ||
for (int i = 0; i < notFound.Length; i++) | ||
result ^= collection.ContainsValue(notFound[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool ImmutableSortedDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _immutableSortedDictionary; | ||
var notFound = _notFound; | ||
for (int i = 0; i < notFound.Length; i++) | ||
result ^= collection.ContainsValue(notFound[i]); | ||
return result; | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/benchmarks/micro/libraries/System.Collections/Contains/ContainsValueTrue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Extensions; | ||
using MicroBenchmarks; | ||
|
||
namespace System.Collections | ||
{ | ||
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)] | ||
[GenericTypeArguments(typeof(int), typeof(int))] // value type | ||
[GenericTypeArguments(typeof(string), typeof(string))] // reference type | ||
public class ContainsValueTrue<TKey, TValue> | ||
{ | ||
private TValue[] _found; | ||
private Dictionary<TKey, TValue> _source; | ||
|
||
private Dictionary<TKey, TValue> _dictionary; | ||
private SortedList<TKey, TValue> _sortedList; | ||
private SortedDictionary<TKey, TValue> _sortedDictionary; | ||
private ImmutableDictionary<TKey, TValue> _immutableDictionary; | ||
private ImmutableSortedDictionary<TKey, TValue> _immutableSortedDictionary; | ||
|
||
[Params(Utils.DefaultCollectionSize)] | ||
public int Size; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_found = ValuesGenerator.ArrayOfUniqueValues<TValue>(Size); | ||
_source = _found.ToDictionary(item => (TKey)(object)item, item => item); | ||
_dictionary = new Dictionary<TKey, TValue>(_source); | ||
_sortedList = new SortedList<TKey, TValue>(_source); | ||
_sortedDictionary = new SortedDictionary<TKey, TValue>(_source); | ||
_immutableDictionary = Immutable.ImmutableDictionary.CreateRange<TKey, TValue>(_source); | ||
_immutableSortedDictionary = Immutable.ImmutableSortedDictionary.CreateRange<TKey, TValue>(_source); | ||
} | ||
|
||
[Benchmark] | ||
public bool Dictionary() | ||
{ | ||
bool result = default; | ||
var collection = _dictionary; | ||
var found = _found; | ||
for (int i = 0; i < found.Length; i++) | ||
result ^= collection.ContainsValue(found[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool SortedList() | ||
{ | ||
bool result = default; | ||
var collection = _sortedList; | ||
var found = _found; | ||
for (int i = 0; i < found.Length; i++) | ||
result ^= collection.ContainsValue(found[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool SortedDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _sortedDictionary; | ||
var found = _found; | ||
for (int i = 0; i < found.Length; i++) | ||
result ^= collection.ContainsValue(found[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool ImmutableDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _immutableDictionary; | ||
var found = _found; | ||
for (int i = 0; i < found.Length; i++) | ||
result ^= collection.ContainsValue(found[i]); | ||
return result; | ||
} | ||
|
||
[Benchmark] | ||
public bool ImmutableSortedDictionary() | ||
{ | ||
bool result = default; | ||
var collection = _immutableSortedDictionary; | ||
var found = _found; | ||
for (int i = 0; i < found.Length; i++) | ||
result ^= collection.ContainsValue(found[i]); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ public class CopyTo<T> | |
private T[] _array; | ||
private List<T> _list; | ||
private ImmutableArray<T> _immutablearray; | ||
private ImmutableList<T> _immutableList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All collection shave the CopyTo method (e. g. dictionaries, sets, etc) but since the original file only went with lists and arrays I also took that approach. Happy to add the rest of the collections if desired |
||
private T[] _destination; | ||
|
||
[GlobalSetup(Targets = new[] { nameof(Array), nameof(Span), nameof(ReadOnlySpan), nameof(Memory), nameof(ReadOnlyMemory) })] | ||
|
@@ -68,5 +69,15 @@ public void SetupImmutableArray() | |
|
||
[Benchmark] | ||
public void ImmutableArray() => _immutablearray.CopyTo(_destination); | ||
|
||
[GlobalSetup(Target = nameof(ImmutableList))] | ||
public void SetupImmutableList() | ||
{ | ||
_immutableList = Immutable.ImmutableList.CreateRange(ValuesGenerator.ArrayOfUniqueValues<T>(Size)); | ||
_destination = new T[Size]; | ||
} | ||
|
||
[Benchmark] | ||
public void ImmutableList() => _immutableList.CopyTo(_destination); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't end up adding TryAdd benchmarks for the immutable collections here because they just use the TryAdd extension which does not work for them.