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>