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

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