Add benchmark method and improve Unsafe collections

Added a new benchmark method `For` in `ParallelNoiseBenchmark` to test gradient noise generation.
Added demonstration code for `UnsafeArray` in `Program.cs` with a custom `Test` struct.

Changed property accessors in `IUnsafeCollection<T>` to use `ref` returns for better memory efficiency.
Changed property accessors and indexers in `UnsafeArray<T>`, `UnsafeList<T>`, and `UnsafeQueue<T>` to return `ref` types.
Changed `TryDequeue` in `UnsafeQueue<T>` to return a default value instead of null.
Changed `Clear` and `Dispose` methods in `UnsafeQueue<T>` to reset size and offset.

Removed commented-out `BenchmarkRunner` code in `Program.cs`.
This commit is contained in:
2025-03-27 12:51:00 +09:00
parent cb69add265
commit 060b4c9477
6 changed files with 140 additions and 33 deletions

View File

@@ -92,4 +92,17 @@ public class ParallelNoiseBenchmark
buffers[i] = NoiseJob.GradientNoise(uv); buffers[i] = NoiseJob.GradientNoise(uv);
}); });
} }
[Benchmark]
public void For()
{
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
for (var i = 0; i < _LENGTH; i++)
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y);
buffers[i] = NoiseJob.GradientNoise(uv);
}
}
} }

View File

@@ -1,4 +1,37 @@
using BenchmarkDotNet.Running; //using BenchmarkDotNet.Running;
using Misaki.HighPerformance.Test; //using Misaki.HighPerformance.Test;
BenchmarkRunner.Run<ParallelNoiseBenchmark>(); //BenchmarkRunner.Run<ParallelNoiseBenchmark>();
using Misaki.HighPerformance.Unsafe.Collections;
using var test = new UnsafeArray<Test>(10, AllocationType.UnInitialized);
for (var i = 0; i < 10; i++)
{
var t = new Test();
t.buffers[0] = i;
test[i] = t;
}
test.ReAlloc(20);
for (var i = 0; i < 10; i++)
{
Console.WriteLine(test[i].buffers[0]);
}
struct Test : IDisposable
{
public UnsafeArray<float> buffers;
public Test()
{
buffers = new UnsafeArray<float>(1, AllocationType.UnInitialized);
}
public void Dispose()
{
buffers.Dispose();
}
}

View File

@@ -3,11 +3,17 @@
public unsafe interface IUnsafeCollection<T> : IDisposable public unsafe interface IUnsafeCollection<T> : IDisposable
where T : unmanaged where T : unmanaged
{ {
public T* Buffer { get; } public T* Buffer
{
get;
}
public int Size { get; } public int Size
{
get;
}
public T this[int index] { get; } public ref T this[int index] { get; }
public void Clear(); public void Clear();
public void ReAlloc(int newSize); public void ReAlloc(int newSize);

View File

@@ -1,8 +1,8 @@
using System.Collections; using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
namespace Misaki.HighPerformance.Unsafe.Collections; namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -45,16 +45,24 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
public T Current public T Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _value; } get
{
return _value;
}
} }
object IEnumerator.Current object IEnumerator.Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return Current; } get
{
return Current;
}
} }
public void Dispose() { } public void Dispose()
{
}
} }
private T* _buffer; private T* _buffer;
@@ -63,7 +71,11 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
public readonly T* Buffer => _buffer; public readonly T* Buffer => _buffer;
public readonly int Size => _size; public readonly int Size => _size;
public readonly T this[int index] => UnsafeUtilities.ReadArrayElement<T>(_buffer, index); public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
}
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this); public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
@@ -72,7 +84,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
public UnsafeArray(int size, AllocationType allocationType) public UnsafeArray(int size, AllocationType allocationType)
{ {
_size = size; _size = size;
_buffer = (T*)NativeMemory.AlignedAlloc((nuint)(size * sizeof(T)), (nuint)AlignOf<T>()); _buffer = (T*)NativeMemory.AlignedAlloc((nuint)(size * sizeof(T)), AlignOf<T>());
if (allocationType == AllocationType.Clear) if (allocationType == AllocationType.Clear)
{ {
@@ -92,7 +104,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear() public void Clear()
{ {
MemClear(_buffer, (uint)(_size * sizeof(T))); MemClear(_buffer, (uint)(_size * sizeof(T)));
} }

View File

@@ -1,7 +1,7 @@
using System.Collections; using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using System.Runtime.CompilerServices;
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers; using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections; namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -44,16 +44,24 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T>
public readonly T Current public readonly T Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _value; } get
{
return _value;
}
} }
readonly object IEnumerator.Current readonly object IEnumerator.Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return Current; } get
{
return Current;
}
} }
public readonly void Dispose() { } public readonly void Dispose()
{
}
} }
/// <summary> /// <summary>
@@ -106,7 +114,11 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T>
public readonly int Size => _size; public readonly int Size => _size;
public readonly int Capacity => _array.Size; public readonly int Capacity => _array.Size;
public readonly T this[int index] => _array[index]; public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _array[index];
}
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this); public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
@@ -128,7 +140,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T>
private readonly void CheckNoResizeCapacity(int count) private readonly void CheckNoResizeCapacity(int count)
{ {
CheckNoResizeCapacity(count, count); CheckNoResizeCapacity(count, Size);
} }
private readonly void CheckNoResizeCapacity(int index, int count) private readonly void CheckNoResizeCapacity(int index, int count)
@@ -268,12 +280,17 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T>
public void ReAlloc(int newSize) public void ReAlloc(int newSize)
{ {
_array.ReAlloc(newSize); _array.ReAlloc(newSize);
if (_size > newSize)
{
_size = newSize;
}
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear()
public readonly void Clear()
{ {
_array.Clear(); _array.Clear();
_size = 0;
} }
public void Dispose() public void Dispose()

View File

@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using Misaki.HighPerformance.Unsafe.Collections.Contracts; using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers; using Misaki.HighPerformance.Unsafe.Helpers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections; namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -11,11 +12,27 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
private int _size; private int _size;
private int _offset; private int _offset;
public T* Buffer => _array.Buffer; public readonly T* Buffer => _array.Buffer;
public int Size => _size; public readonly int Size => _size;
public int Capacity => _array.Size; public readonly int Capacity => _array.Size;
public T this[int index] => _array[index]; public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _array[index];
}
public UnsafeQueue(int capacity, AllocationType allocationType)
{
_array = new UnsafeArray<T>(capacity, allocationType);
_size = 0;
_offset = 0;
if (allocationType == AllocationType.Clear)
{
Clear();
}
}
public void Enqueue(T value) public void Enqueue(T value)
{ {
@@ -42,11 +59,11 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
return value; return value;
} }
public bool TryDequeue([MaybeNullWhen(false)] out T? value) public bool TryDequeue([MaybeNullWhen(false)] out T value)
{ {
if (_offset > _size) if (_size == 0)
{ {
value = null; value = default;
return false; return false;
} }
@@ -57,15 +74,24 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
public void ReAlloc(int newSize) public void ReAlloc(int newSize)
{ {
_array.ReAlloc(newSize); _array.ReAlloc(newSize);
if (_size > newSize)
{
_size = newSize;
}
} }
public void Clear() public void Clear()
{ {
_array.Clear(); _array.Clear();
_size = 0;
_offset = 0;
} }
public void Dispose() public void Dispose()
{ {
_array.Dispose(); _array.Dispose();
_size = 0;
_offset = 0;
} }
} }