Compare commits

...

2 Commits

Author SHA1 Message Date
ikpil b81a5923a3 benchmark arrays 2024-02-24 20:36:33 +09:00
ikpil 3fbfb968d0 test RcRentedArray 2024-02-24 20:00:51 +09:00
3 changed files with 116 additions and 18 deletions

View File

@ -6,7 +6,7 @@ namespace DotRecast.Core.Buffers
{
public static class RcRentedArray
{
public static RcRentedArray<T> RentDisposableArray<T>(int minimumLength)
public static RcRentedArray<T> Rent<T>(int minimumLength)
{
var array = ArrayPool<T>.Shared.Rent(minimumLength);
return new RcRentedArray<T>(ArrayPool<T>.Shared, array, minimumLength);
@ -17,44 +17,41 @@ namespace DotRecast.Core.Buffers
{
private ArrayPool<T> _owner;
private T[] _array;
private readonly RcAtomicInteger _disposed;
public int Length { get; }
public bool IsDisposed => null == _owner || null == _array;
internal RcRentedArray(ArrayPool<T> owner, T[] array, int length)
{
_owner = owner;
_array = array;
Length = length;
_disposed = new RcAtomicInteger(0);
}
public T this[int index]
public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length);
return _array[index];
return ref _array[index];
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length);
_array[index] = value;
}
public T[] AsArray()
{
return _array;
}
public void Dispose()
{
if (1 != _disposed.IncrementAndGet())
return;
_owner?.Return(_array, true);
_array = null;
_owner = null;
if (null != _owner && null != _array)
{
_owner.Return(_array, true);
_owner = null;
_array = null;
}
}
}
}

View File

@ -0,0 +1,101 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections;
using NUnit.Framework;
namespace DotRecast.Core.Test;
public class RcArrayBenchmarkTests
{
private const int StepLength = 512;
private const int RandomLoop = 1000;
private readonly RcRand _rand = new RcRand();
private (string title, long ticks) Bench(string title, Action<int> source)
{
var begin = RcFrequency.Ticks;
for (int step = StepLength; step > 0; --step)
{
for (int i = 0; i < RandomLoop; ++i)
{
source.Invoke(step);
}
}
var end = RcFrequency.Ticks - begin;
return (title, end);
}
private void RoundForArray(int len)
{
var array = new int[len];
for (int ii = 0; ii < len; ++ii)
{
array[ii] = _rand.NextInt32();
}
}
private void RoundForPureRentArray(int len)
{
var array = ArrayPool<int>.Shared.Rent(len);
for (int ii = 0; ii < array.Length; ++ii)
{
array[ii] = _rand.NextInt32();
}
ArrayPool<int>.Shared.Return(array);
}
private void RoundForRcRentedArray(int len)
{
using var rentedArray = RcRentedArray.Rent<int>(len);
var array = rentedArray.AsArray();
for (int i = 0; i < rentedArray.Length; ++i)
{
array[i] = _rand.NextInt32();
}
}
private void RoundForRcStackArray(int len)
{
var array = new RcStackArray512<int>();
for (int ii = 0; ii < len; ++ii)
{
array[ii] = _rand.NextInt32();
}
}
private void RoundForStackalloc(int len)
{
Span<int> array = stackalloc int[len];
for (int ii = 0; ii < len; ++ii)
{
array[ii] = _rand.NextInt32();
}
}
[Test]
public void TestBenchmarkArrays()
{
var list = new List<(string title, long ticks)>();
list.Add(Bench("new int[len]", RoundForArray));
list.Add(Bench("ArrayPool<int>.Shared.Rent(len)", RoundForPureRentArray));
list.Add(Bench("RcRentedArray.Rent<int>(len)", RoundForRcRentedArray));
list.Add(Bench("new RcStackArray512<int>()", RoundForRcStackArray));
list.Add(Bench("stackalloc int[len]", RoundForStackalloc));
list.Sort((x, y) => x.ticks.CompareTo(y.ticks));
foreach (var t in list)
{
Console.WriteLine($"{t.title} {t.ticks / (double)TimeSpan.TicksPerMillisecond} ms");
}
}
}

View File

@ -31,7 +31,7 @@ public class RcRentedArrayTest
{
int length = Math.Max(2, (int)(rand.Next() * 2048));
var values = RandomValues(length);
using var array = RcRentedArray.RentDisposableArray<int>(length);
using var array = RcRentedArray.Rent<int>(length);
for (int i = 0; i < array.Length; ++i)
{