Performance & standardization improvements.
Business logic fixes. RecyclableQueue & RecyclableStack added - pending improvements.
This commit is contained in:
1 parent
4e5835b20e
commit
aba28028b1
17 files changed
+1269
-190
No files matched your search
@@ -0,0 +1,253 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using MiscUtil;
|
||||
using MiscUtil.Extensions;
|
||||
|
||||
namespace Recyclable.Collections.Benchmarks
|
||||
{
|
||||
public delegate T Compare<T>(T x, T y);
|
||||
|
||||
public readonly struct MyComparerReadOnlyStruct<T> : IComparer<T>
|
||||
{
|
||||
public int Compare(T? x, T? y) => Operator.Subtract(x, y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
public struct MyComparerStruct<T> : IComparer<T>
|
||||
{
|
||||
public int Compare(T? x, T? y) => Operator.Subtract(x, y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
public class MyComparer : IComparer<long>
|
||||
{
|
||||
public int Compare(long x, long y) => (x - y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class DelegateVsComparerBenchmark
|
||||
{
|
||||
private static readonly Comparer<long> _comparer = Comparer<long>.Default;
|
||||
|
||||
private const int _objectCount = 1_000_000;
|
||||
private static readonly IEnumerable<long> _testObjects = Enumerable.Range(1, _objectCount).Select(x => (long)x);
|
||||
private static readonly IEnumerable<Func<long, long, int>> _testListOfFunctions = _testObjects.Select<long, Func<long, long, int>>(static x => new MyComparer().Compare);
|
||||
private static readonly IEnumerable<IComparer<long>> _testListOfComparers = _testObjects.Select(x => (IComparer<long>)new MyComparer());
|
||||
private static readonly IEnumerable<Compare<long>> _testListOfDelegates = _testObjects.Select(x => new Compare<long>((x, y) => new MyComparer().Compare(x, y)));
|
||||
private static readonly IEnumerable<Func<long, long, int>> _testListOfLocalFunctions = GetLocalFunctions(_testObjects);
|
||||
private static readonly IEnumerable<MyComparer> _testListOfClasses = _testObjects.Select(x => new MyComparer());
|
||||
private static readonly IEnumerable<MyComparerStruct<long>> _testListOfStructs = _testObjects.Select(x => new MyComparerStruct<long>());
|
||||
private static readonly IEnumerable<IComparer<long>> _testListOfBoxedStructs = _testObjects.Select(x => (IComparer<long>)new MyComparerStruct<long>());
|
||||
private static IEnumerable<TComparer> _testListOfUnboxedStructs<TComparer, TValue>() where TComparer : IComparer<TValue>, new()
|
||||
{
|
||||
foreach (var item in _testObjects)
|
||||
{
|
||||
yield return new TComparer();
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Func<long, long, int>> GetLocalFunctions(IEnumerable<long> testObjects)
|
||||
{
|
||||
static int compareFunc(long x, long y)
|
||||
{
|
||||
return (x - y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var item in testObjects)
|
||||
{
|
||||
yield return compareFunc;
|
||||
}
|
||||
}
|
||||
|
||||
private static void TestWithComparer(IComparer<long> comparer)
|
||||
{
|
||||
var testData = _testObjects.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
|
||||
{
|
||||
_ = comparer.Compare(testData[itemIdx], testData[itemIdx + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public void Comparer()
|
||||
{
|
||||
TestWithComparer(_comparer);
|
||||
}
|
||||
|
||||
private static void TestWithFunc(Func<long, long, int> compareFunc)
|
||||
{
|
||||
var testData = _testObjects.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
|
||||
{
|
||||
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Func()
|
||||
{
|
||||
TestWithFunc((x, y) => (x - y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0
|
||||
});
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void StaticFunc()
|
||||
{
|
||||
TestWithFunc(static (x, y) => (x - y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0
|
||||
});
|
||||
}
|
||||
|
||||
private static void TestWithDelegate<T>(Compare<long> compareFunc)
|
||||
{
|
||||
var testData = _testObjects.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
|
||||
{
|
||||
_ = compareFunc(testData[itemIdx], testData[itemIdx + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Delegate()
|
||||
{
|
||||
TestWithDelegate<long>((x, y) => (x - y) switch
|
||||
{
|
||||
> 0 => 1,
|
||||
< 0 => -1,
|
||||
_ => 0
|
||||
});
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfFuncs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfFunctions.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var compareFunc = testData[itemIdx];
|
||||
_ = compareFunc(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfLocalFuncs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfLocalFunctions.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var compareFunc = testData[itemIdx];
|
||||
_ = compareFunc(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfComparers()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfComparers.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfClasses()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfClasses.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfStructs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfStructs.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfBoxedStructs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfBoxedStructs.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfUnboxedStructs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfUnboxedStructs<MyComparerStruct<long>, long>().ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfUnboxedReadOnlyStructs()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfUnboxedStructs<MyComparerReadOnlyStruct<long>, long>().ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var comparer = testData[itemIdx];
|
||||
_ = comparer.Compare(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ListOfDelegates()
|
||||
{
|
||||
const long x = 2, y = 5;
|
||||
var testData = _testListOfDelegates.ToArray();
|
||||
for (var itemIdx = 0; itemIdx < testData.Length; itemIdx++)
|
||||
{
|
||||
var compareDelegate = testData[itemIdx];
|
||||
_ = compareDelegate(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
|
||||
namespace Recyclable.Collections.Benchmarks
|
||||
{
|
||||
[MemoryDiagnoser]
|
||||
public class ListVsLinkedListBenchmark
|
||||
{
|
||||
private const int _objectCount = 1_000_000;
|
||||
private const int _blockSize = 100_000;
|
||||
private static readonly object[] _testObjects = Enumerable.Range(1, _objectCount).Select(x => new object()).ToArray();
|
||||
|
||||
private static void DoNothing<T>(T obj)
|
||||
{
|
||||
_ = obj;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Array()
|
||||
{
|
||||
var list = new object[_testObjects.LongLength];
|
||||
for (var i = 0; i < _testObjects.Length; i++)
|
||||
{
|
||||
list[i] = _testObjects[i];
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.LongLength; i++)
|
||||
{
|
||||
DoNothing(list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public void RecyclableList()
|
||||
{
|
||||
using var list = new RecyclableList<object>(_blockSize);
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Add(_testObjects[i]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
DoNothing(_testObjects[i]);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void List()
|
||||
{
|
||||
var list = new List<object>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Add(_testObjects[i]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
DoNothing(list[i]);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void LinkedList()
|
||||
{
|
||||
var list = new LinkedList<object>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
_ = list.AddLast(_testObjects[i]);
|
||||
}
|
||||
|
||||
foreach (var node in list)
|
||||
{
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void RecyclableQueue()
|
||||
{
|
||||
var list = new RecyclableQueue<object>(_blockSize);
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Enqueue(_testObjects[i]);
|
||||
}
|
||||
|
||||
foreach (var node in list)
|
||||
{
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void Queue()
|
||||
{
|
||||
var list = new Queue<object>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Enqueue(_testObjects[i]);
|
||||
}
|
||||
|
||||
foreach (var node in list)
|
||||
{
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void PriorityQueue()
|
||||
{
|
||||
var list = new PriorityQueue<object, long>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Enqueue(_testObjects[i], i);
|
||||
}
|
||||
|
||||
while (list.Count > 0)
|
||||
{
|
||||
var node = list.Dequeue();
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void RecyclableSortedList_InUpdateMode()
|
||||
{
|
||||
var list = new RecyclableSortedList<long, object>(_blockSize);
|
||||
list.BeginUpdate();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Add(i, _testObjects[i]);
|
||||
}
|
||||
|
||||
list.EndUpdate();
|
||||
|
||||
for (var i = 0; i < list.LongCount; i++)
|
||||
{
|
||||
DoNothing(_testObjects[i]);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void RecyclableSortedList()
|
||||
{
|
||||
var list = new RecyclableSortedList<long, object>(_blockSize);
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Add(i, _testObjects[i]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.LongCount; i++)
|
||||
{
|
||||
DoNothing(_testObjects[i]);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void SortedList()
|
||||
{
|
||||
var list = new SortedList<long, object>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Add(i, _testObjects[i]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
DoNothing(_testObjects[i]);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void RecyclableStack()
|
||||
{
|
||||
var list = new RecyclableStack<object>(_blockSize);
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Push(_testObjects[i]);
|
||||
}
|
||||
|
||||
foreach (var node in list)
|
||||
{
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
public void Stack()
|
||||
{
|
||||
var list = new Stack<object>();
|
||||
for (var i = 0; i < _testObjects.LongLength; i++)
|
||||
{
|
||||
list.Push(_testObjects[i]);
|
||||
}
|
||||
|
||||
foreach (var node in list)
|
||||
{
|
||||
DoNothing(node);
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using BenchmarkDotNet.Running;
|
||||
using Recyclable.Collections.Benchmarks;
|
||||
|
||||
BenchmarkRunner.Run<ListVsLinkedListBenchmark>();
|
||||
//BenchmarkRunner.Run<DelegateVsComparerBenchmark>();
|
||||
|
||||
//var tests = new ListVsLinkedListBenchmark();
|
||||
//tests.RecyclableList();
|
||||
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.3" />
|
||||
<PackageReference Include="JonSkeet.MiscUtil" Version="0.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Recyclable.Collections\Recyclable.Collections.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user