Refactor unsafe collections and benchmarks

Changed the `CollectionBenchmark` class to use unsafe code for improved memory operations and added benchmarks for stack-allocated arrays.
Changed the `ParallelNoiseBenchmark` class to remove the internal `NoiseJob` struct, promoting better organization.
Changed the `AllocationManager` class to remove the lock mechanism for thread safety and simplified the `Reset` method.
Changed the `Arena` and `DynamicArena` structs to include `Initialize` methods for better initialization control.
Changed the `UnsafeArray<T>`, `UnsafeHashSet<T>`, and `UnsafeList<T>` structs to improve element access and management.
Updated the `UnsafeCollectionExtensions` class to enhance usability with new methods for copying and converting collections.
Updated the `MemoryLeakException` class to provide more detailed stack trace information for better debugging.
Removed the usage of `UnsafeHashMap` in `Program.cs` and directly ran the `CollectionBenchmark`.
Added a new `NoiseJob` struct in `NoiseJob.cs` for generating gradient noise using `UnsafeArray<float>`.
Fixed minor typos and improved method signatures throughout the codebase for clarity.
This commit is contained in:
2025-04-11 15:53:11 +09:00
parent 463735a481
commit 691a336111
13 changed files with 217 additions and 160 deletions

View File

@@ -1,4 +1,4 @@
#define UNSAFE_COLLECTION_CHECK
//#define UNSAFE_COLLECTION_CHECK
using Misaki.HighPerformance.Unsafe.Collections;
#if UNSAFE_COLLECTION_CHECK
@@ -21,7 +21,7 @@ public static unsafe class AllocationManager
private static Dictionary<IntPtr, MemoryLeakExceptionInfo> _allocated = null!;
#endif
private static readonly Lock _lock = new();
//private static readonly Lock _lock = new();
/// <summary>
/// Initializes the AllocationManager with a specified initial size for the memory arena.
@@ -42,7 +42,7 @@ public static unsafe class AllocationManager
_initialized = true;
}
internal static T* Allocate<T>(uint size, uint alignSize, Allocator allocator, AllocationOption allocationOption)
public static T* Allocate<T>(uint size, uint alignSize, Allocator allocator, AllocationOption allocationOption)
where T : unmanaged
{
if (allocationOption.HasFlag(AllocationOption.UnTracked))
@@ -55,45 +55,45 @@ public static unsafe class AllocationManager
Initialize();
}
lock (_lock)
//lock (_lock)
//{
T* buffer;
switch (allocator)
{
T* buffer;
switch (allocator)
{
case Allocator.Temp:
buffer = (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, allocationOption);
break;
case Allocator.Temp:
buffer = (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, allocationOption);
break;
case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T);
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T);
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
#if UNSAFE_COLLECTION_CHECK
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
{
Size = allocationSize,
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
{
Size = allocationSize,
#if DEBUG
StackTrace = new StackTrace(true)
StackTrace = new StackTrace(true)
#endif
};
};
#endif
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(buffer, allocationSize);
}
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(buffer, allocationSize);
}
break;
break;
default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
}
return buffer;
default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
}
return buffer;
//}
}
internal static T* Realloc<T>(T* buffer, uint size, uint alignSize, Allocator allocator)
public static T* Realloc<T>(T* buffer, uint size, uint alignSize, Allocator allocator)
where T : unmanaged
{
if (!_initialized)
@@ -101,68 +101,62 @@ public static unsafe class AllocationManager
throw new InvalidOperationException("The AllocationManager has not been initialized.");
}
lock (_lock)
//lock (_lock)
//{
T* newBuffer;
switch (allocator)
{
T* newBuffer;
switch (allocator)
{
case Allocator.Temp:
newBuffer = (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, AllocationOption.None);
break;
case Allocator.Temp:
newBuffer = (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, AllocationOption.None);
break;
case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T);
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize);
case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T);
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize);
#if UNSAFE_COLLECTION_CHECK
// If the allocation map can not find the old value, it means that it was a untracked allocation
if (_allocated.Remove((IntPtr)buffer))
// If the allocation map can not find the old value, it means that it was a untracked allocation
if (_allocated.Remove((IntPtr)buffer))
{
_allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
{
_allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
{
Size = allocationSize,
Size = allocationSize,
#if DEBUG
StackTrace = new StackTrace(true)
StackTrace = new StackTrace(true)
#endif
};
}
};
}
#endif
break;
break;
default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
}
return newBuffer;
default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
}
return newBuffer;
//}
}
internal static void Free(void* ptr, Allocator allocator)
public static void Free(void* ptr, Allocator allocator)
{
lock (_lock)
//lock (_lock)
//{
if (allocator == Allocator.Persistent)
{
if (allocator == Allocator.Persistent)
{
AlignedFree(ptr);
AlignedFree(ptr);
#if UNSAFE_COLLECTION_CHECK
_allocated.Remove((IntPtr)ptr);
_allocated.Remove((IntPtr)ptr);
#endif
}
}
//}
}
/// <summary>
/// Resets the memory arena, optionally clearing the allocated memory.
/// </summary>
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
public static void Reset(bool clear = false)
public static void Reset()
{
if (!_initialized)
{
throw new InvalidOperationException("The AllocationManager has not been initialized.");
}
_arena.Reset(clear);
_arena.Reset();
}
/// <summary>

View File

@@ -15,6 +15,16 @@ public unsafe struct Arena : IDisposable
public Arena(uint size)
{
Initialize(size);
}
public void Initialize(uint size)
{
if (_buffer != null)
{
return;
}
_buffer = (byte*)Malloc(size);
_size = size;
_offset = 0;
@@ -32,7 +42,10 @@ public unsafe struct Arena : IDisposable
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void* Allocate(uint size, uint alignSize, AllocationOption allocationOption)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_disposed)
{
throw new ObjectDisposedException(nameof(DynamicArena));
}
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
if (offset + size > _size)
@@ -56,13 +69,11 @@ public unsafe struct Arena : IDisposable
/// </summary>
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void Reset(bool clear = false)
public void Reset()
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (clear)
if (_disposed)
{
MemClear(_buffer, _size);
throw new ObjectDisposedException(nameof(DynamicArena));
}
_offset = 0;

View File

@@ -16,8 +16,7 @@ public unsafe struct DynamicArena : IDisposable
private ArenaNode* _root;
private ArenaNode* _current;
private readonly uint _initialSize;
private bool _disposed;
private uint _initialSize;
/// <summary>
/// Initializes a new instance of DynamicArena with the specified initial size.
@@ -32,11 +31,25 @@ public unsafe struct DynamicArena : IDisposable
_current = _root;
}
public void Initialize(uint initialSize)
{
if (_root != null)
{
return;
}
_initialSize = initialSize;
_root = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
_root->arena = new Arena(initialSize);
_root->next = null;
_current = _root;
}
private bool CreateNewNode(uint size)
{
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
try
{
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
newNode->arena = new Arena(size);
newNode->next = null;
@@ -46,6 +59,7 @@ public unsafe struct DynamicArena : IDisposable
}
catch
{
Free(newNode);
return false;
}
}
@@ -59,7 +73,10 @@ public unsafe struct DynamicArena : IDisposable
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void* Allocate(uint size, uint alignSize, AllocationOption allocationType)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_root == null)
{
return null;
}
void* result = null;
var current = _current;
@@ -89,14 +106,12 @@ public unsafe struct DynamicArena : IDisposable
/// </summary>
/// <param name="clear">If true, memory will be cleared during reset.</param>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void Reset(bool clear = false)
public void Reset()
{
ObjectDisposedException.ThrowIf(_disposed, this);
var current = _root;
while (current != null)
{
current->arena.Reset(clear);
current->arena.Reset();
current = current->next;
}
@@ -108,7 +123,7 @@ public unsafe struct DynamicArena : IDisposable
/// </summary>
public void Dispose()
{
if (_disposed)
if (_root == null)
{
return;
}
@@ -124,6 +139,5 @@ public unsafe struct DynamicArena : IDisposable
_root = null;
_current = null;
_disposed = true;
}
}

View File

@@ -73,7 +73,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
get => ref _buffer[index];
}
public readonly bool IsCreated
@@ -128,6 +128,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_allocator = Allocator.External;
}
/// <inheritdoc/>
public void Resize(int newSize)
{
if (newSize == _count)
@@ -139,20 +140,28 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_count = newSize;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear()
{
MemClear(_buffer, (nuint)(_count * sizeof(T)));
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr()
{
return _buffer;
}
/// <inheritdoc/>
public void Dispose()
{
if (!IsCreated)
{
return;
}
AllocationManager.Free(_buffer, _allocator);
_buffer = null;

View File

@@ -49,7 +49,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
public IEnumerator<T> GetEnumerator() => new Enumerator((HashMapHelper<T>*)UnsafeUtilities.AddressOf(ref _hashMap));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption)
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, allocator, allocationOption);
}

View File

@@ -118,10 +118,12 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
public readonly int Capacity => _array.Count;
public readonly bool IsCreated => _array.IsCreated;
public readonly ref T this[int index]
public readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _array[index];
get => _array[index];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _array[index] = value;
}
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));

View File

@@ -70,10 +70,12 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
public readonly int Capacity => _array.Count;
public readonly bool IsCreated => _array.IsCreated;
public readonly ref T this[int index]
public readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _array[index];
get => _array[index];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _array[index] = value;
}
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));

View File

@@ -37,7 +37,7 @@ public class MemoryLeakException(params MemoryLeakExceptionInfo[] Infos) : Excep
var frame = stackTrace.GetFrame(i);
if (frame != null)
{
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Line: {frame.GetFileLineNumber()}");
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
}
}

View File

@@ -15,7 +15,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="source">Represents the source collection from which elements are copied.</param>
/// <param name="destination">Represents the target span where elements are copied to.</param>
/// <exception cref="ArgumentException">Thrown when the sizes of the source collection and destination span do not match.</exception>
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination) where T : unmanaged
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination)
where T : unmanaged
{
if (source.Count > destination.Length)
{
@@ -38,7 +39,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="destinationIndex">The starting index in the destination span where the elements will be placed.</param>
/// <param name="length">The number of elements to copy from the source to the destination.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source collection or destination span.</exception>
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination, int sourceIndex, int destinationIndex, int length) where T : unmanaged
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination, int sourceIndex, int destinationIndex, int length)
where T : unmanaged
{
if (sourceIndex + length > source.Count || destinationIndex + length > destination.Length)
{
@@ -58,7 +60,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="destination">Represents the unsafe collection that will receive the copied elements.</param>
/// <param name="source">Represents the span containing the elements to be copied to the unsafe collection.</param>
/// <exception cref="ArgumentException">Thrown when the source span and destination collection have different sizes.</exception>
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source) where T : unmanaged
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source)
where T : unmanaged
{
if (destination.Count > source.Length)
{
@@ -81,7 +84,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="destinationIndex">The starting index in the destination collection where the elements will be placed.</param>
/// <param name="length">The number of elements to copy from the source span to the destination collection.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source span or destination collection.</exception>
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source, int sourceIndex, int destinationIndex, int length) where T : unmanaged
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source, int sourceIndex, int destinationIndex, int length)
where T : unmanaged
{
if (sourceIndex + length > source.Length || destinationIndex + length > destination.Count)
{
@@ -100,7 +104,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
/// <param name="source">The UnsafeCollection instance that contains the data to be converted.</param>
/// <returns>A new collection containing the elements from the UnsafeCollection.</returns>
public static T[] ToArray<T>(this IUnsafeCollection<T> source) where T : unmanaged
public static T[] ToArray<T>(this IUnsafeCollection<T> source)
where T : unmanaged
{
var array = new T[source.Count];
fixed (T* ptr = array)
@@ -117,7 +122,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
/// <param name="source">The collection from which elements are copied to create the new list.</param>
/// <returns>A list containing the elements from the specified unmanaged collection.</returns>
public static List<T> ToList<T>(this IUnsafeCollection<T> source) where T : unmanaged
public static List<T> ToList<T>(this IUnsafeCollection<T> source)
where T : unmanaged
{
var list = new List<T>(source.Count);
fixed (T* ptr = list.ToArray())
@@ -133,7 +139,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <typeparam name="T">Represents a type that can be stored in unmanaged memory.</typeparam>
/// <param name="source">The UnsafeCollection instance to be converted into a Span.</param>
/// <returns>A Span that provides a view over the elements of the UnsafeCollection.</returns>
public static Span<T> AsSpan<T>(this IUnsafeCollection<T> source) where T : unmanaged
public static Span<T> AsSpan<T>(this IUnsafeCollection<T> source)
where T : unmanaged
{
return new(source.GetUnsafePtr(), source.Count);
}
@@ -145,7 +152,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="source">The collection to search for the specified value.</param>
/// <param name="value">The value to locate within the collection.</param>
/// <param name="index">Outputs the index of the found value or -1 if not found.</param>
public static void IndexOf<T>(this IUnsafeCollection<T> source, T value, out int index) where T : unmanaged, IEquatable<T>
public static void IndexOf<T>(this IUnsafeCollection<T> source, T value, out int index)
where T : unmanaged, IEquatable<T>
{
for (var i = 0; i < source.Count; i++)
{
@@ -165,7 +173,8 @@ public unsafe static class UnsafeCollectionExtensions
/// <param name="source">The collection being searched for the specified value.</param>
/// <param name="value">The value being searched for within the collection.</param>
/// <returns>Returns true if the value is found; otherwise, returns false.</returns>
public static bool Conations<T>(this IUnsafeCollection<T> source, T value) where T : unmanaged, IEquatable<T>
public static bool Conations<T>(this IUnsafeCollection<T> source, T value)
where T : unmanaged, IEquatable<T>
{
source.IndexOf(value, out var index);
return index != -1;