-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net8.0;net6.0;net7.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BetterCollections\BetterCollections.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" /> | ||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.11" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,77 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BetterCollections; | ||
|
||
namespace Benchmark; | ||
|
||
[MemoryDiagnoser] | ||
[RPlotExporter] | ||
public class BenchmarkFlatHashMap_Add1000 | ||
{ | ||
[Benchmark] | ||
public FlatHashMap<int, int> FlatHashMap() | ||
{ | ||
var map = new FlatHashMap<int, int>(); | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
map.Add(i, i); | ||
} | ||
return map; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public Dictionary<int, int> Dictionary() | ||
{ | ||
var map = new Dictionary<int, int>(); | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
map.Add(i, i); | ||
} | ||
return map; | ||
} | ||
} | ||
|
||
[MemoryDiagnoser] | ||
[RPlotExporter] | ||
public class BenchmarkFlatHashMap_Get1000 | ||
{ | ||
private FlatHashMap<int, int> flatHashMap = new(); | ||
private Dictionary<int, int> dictionary = new(); | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
flatHashMap = new FlatHashMap<int, int>(); | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
flatHashMap.Add(i, i); | ||
} | ||
|
||
dictionary = new Dictionary<int, int>(); | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
dictionary.Add(i, i); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public int FlatHashMap() | ||
{ | ||
var a = 0; | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
a += flatHashMap[i]; | ||
} | ||
return a; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public int Dictionary() | ||
{ | ||
var a = 0; | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
a += dictionary[i]; | ||
} | ||
return a; | ||
} | ||
} |
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,3 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
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
12 changes: 12 additions & 0 deletions
12
BetterCollections/Exceptions/UnexpectedConcurrentException.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,12 @@ | ||
using System; | ||
|
||
namespace BetterCollections.Exceptions; | ||
|
||
public class UnexpectedConcurrentException : Exception | ||
{ | ||
public UnexpectedConcurrentException() : this( | ||
"A concurrent call occurred on a container that does not allow concurrency, and the internal state may have been corrupted") { } | ||
|
||
public UnexpectedConcurrentException(string message) : base(message) { } | ||
public UnexpectedConcurrentException(string message, Exception inner) : base(message, inner) { } | ||
} |
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,45 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace BetterCollections.Misc; | ||
|
||
public interface IGetter<out T, S> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public T Get(in S source); | ||
} | ||
|
||
public readonly struct IdentityGetter<T> : IGetter<T, T> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public T Get(in T source) => source; | ||
} | ||
|
||
public readonly struct KeyValueGetter<K, V> : IGetter<KeyValuePair<K, V>, (K Key, V Value)> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public KeyValuePair<K, V> Get(in (K Key, V Value) source) | ||
=> new(source.Key, source.Value); | ||
} | ||
|
||
public readonly struct KeyGetter<K, V> : IGetter<K, (K Key, V Value)>, IGetter<K, KeyValuePair<K, V>> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public K Get(in (K Key, V Value) source) | ||
=> source.Key; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public K Get(in KeyValuePair<K, V> source) | ||
=> source.Key; | ||
} | ||
|
||
public readonly struct ValueGetter<K, V> : IGetter<V, (K Key, V Value)>, IGetter<V, KeyValuePair<K, V>> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public V Get(in (K Key, V Value) source) | ||
=> source.Value; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public V Get(in KeyValuePair<K, V> source) | ||
=> source.Value; | ||
} |
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