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:
@@ -3,11 +3,17 @@
|
||||
public unsafe interface IUnsafeCollection<T> : IDisposable
|
||||
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 ReAlloc(int newSize);
|
||||
|
||||
@@ -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.InteropServices;
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
@@ -45,16 +45,24 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
public T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get { return _value; }
|
||||
get
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
}
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get { return Current; }
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private T* _buffer;
|
||||
@@ -63,7 +71,11 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
public readonly T* Buffer => _buffer;
|
||||
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);
|
||||
|
||||
@@ -72,7 +84,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
public UnsafeArray(int size, AllocationType allocationType)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
@@ -92,7 +104,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Clear()
|
||||
public void Clear()
|
||||
{
|
||||
MemClear(_buffer, (uint)(_size * sizeof(T)));
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
@@ -11,11 +12,27 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
private int _size;
|
||||
private int _offset;
|
||||
|
||||
public T* Buffer => _array.Buffer;
|
||||
public int Size => _size;
|
||||
public int Capacity => _array.Size;
|
||||
public readonly T* Buffer => _array.Buffer;
|
||||
public readonly int Size => _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)
|
||||
{
|
||||
@@ -42,11 +59,11 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -57,15 +74,24 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
||||
public void ReAlloc(int newSize)
|
||||
{
|
||||
_array.ReAlloc(newSize);
|
||||
|
||||
if (_size > newSize)
|
||||
{
|
||||
_size = newSize;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_array.Clear();
|
||||
_size = 0;
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_array.Dispose();
|
||||
_size = 0;
|
||||
_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user