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:
@@ -5,28 +5,45 @@ using Misaki.HighPerformance.Unsafe.Collections;
|
|||||||
namespace Misaki.HighPerformance.Test;
|
namespace Misaki.HighPerformance.Test;
|
||||||
|
|
||||||
[MemoryDiagnoser]
|
[MemoryDiagnoser]
|
||||||
public class CollectionBenchmark
|
public unsafe class CollectionBenchmark
|
||||||
{
|
{
|
||||||
[Params(10, 100, 1000)]
|
[Params(10, 100, 1000)]
|
||||||
public int count = 100;
|
public int count;
|
||||||
|
|
||||||
[GlobalSetup]
|
[GlobalSetup]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
AllocationManager.Initialize(512_000);
|
AllocationManager.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public void Array()
|
public void Array()
|
||||||
{
|
{
|
||||||
var array = new int[count];
|
var array = new int[count];
|
||||||
|
for (var i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
array[i] = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark(Baseline = true)]
|
||||||
public void UnsafeArray()
|
public void UnsafeArray()
|
||||||
{
|
{
|
||||||
var array = new UnsafeArray<int>(count, Allocator.Temp);
|
var array = new UnsafeArray<int>(count, Allocator.Temp);
|
||||||
AllocationManager.Reset();
|
for (var i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
array[i] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Benchmark]
|
||||||
|
public void StackArray()
|
||||||
|
{
|
||||||
|
var array = stackalloc int[count];
|
||||||
|
for (var i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
array[i] = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[GlobalCleanup]
|
[GlobalCleanup]
|
||||||
|
|||||||
50
Misaki.HighPerformance.Test/Jobs/NoiseJob.cs
Normal file
50
Misaki.HighPerformance.Test/Jobs/NoiseJob.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using Misaki.HighPerformance.Jobs;
|
||||||
|
using Misaki.HighPerformance.Unsafe.Collections;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Misaki.HighPerformance.Test.Jobs;
|
||||||
|
internal struct NoiseJob : IJobParallelFor
|
||||||
|
{
|
||||||
|
public UnsafeArray<float> buffers;
|
||||||
|
public int width;
|
||||||
|
public int height;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static float Frac(float x)
|
||||||
|
{
|
||||||
|
return x - MathF.Truncate(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Vector2 GradientNoiseDirect(Vector2 uv)
|
||||||
|
{
|
||||||
|
uv.X %= 289;
|
||||||
|
uv.Y %= 289;
|
||||||
|
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
|
||||||
|
x = (34 * x + 1) * x % 289;
|
||||||
|
x = Frac(x / 41) * 2 - 1;
|
||||||
|
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float GradientNoise(Vector2 uv)
|
||||||
|
{
|
||||||
|
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
|
||||||
|
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
|
||||||
|
|
||||||
|
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
|
||||||
|
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
|
||||||
|
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
|
||||||
|
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
|
||||||
|
|
||||||
|
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
|
||||||
|
return float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Execute(int index)
|
||||||
|
{
|
||||||
|
var x = index % width;
|
||||||
|
var y = index / height;
|
||||||
|
var uv = new Vector2(x, y);
|
||||||
|
buffers[index] = GradientNoise(uv);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,59 +1,14 @@
|
|||||||
using BenchmarkDotNet.Attributes;
|
using BenchmarkDotNet.Attributes;
|
||||||
using Misaki.HighPerformance.Jobs;
|
using Misaki.HighPerformance.Jobs;
|
||||||
|
using Misaki.HighPerformance.Test.Jobs;
|
||||||
using Misaki.HighPerformance.Unsafe.Collections;
|
using Misaki.HighPerformance.Unsafe.Collections;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Misaki.HighPerformance.Test;
|
namespace Misaki.HighPerformance.Test;
|
||||||
|
|
||||||
[MemoryDiagnoser]
|
[MemoryDiagnoser]
|
||||||
public class ParallelNoiseBenchmark
|
public class ParallelNoiseBenchmark
|
||||||
{
|
{
|
||||||
private struct NoiseJob : IJobParallelFor
|
|
||||||
{
|
|
||||||
public UnsafeArray<float> buffers;
|
|
||||||
public int width;
|
|
||||||
public int height;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static float Frac(float x)
|
|
||||||
{
|
|
||||||
return x - MathF.Truncate(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Vector2 GradientNoiseDirect(Vector2 uv)
|
|
||||||
{
|
|
||||||
uv.X %= 289;
|
|
||||||
uv.Y %= 289;
|
|
||||||
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
|
|
||||||
x = (34 * x + 1) * x % 289;
|
|
||||||
x = Frac(x / 41) * 2 - 1;
|
|
||||||
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static float GradientNoise(Vector2 uv)
|
|
||||||
{
|
|
||||||
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
|
|
||||||
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
|
|
||||||
|
|
||||||
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
|
|
||||||
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
|
|
||||||
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
|
|
||||||
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
|
|
||||||
|
|
||||||
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
|
|
||||||
return float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Execute(int index)
|
|
||||||
{
|
|
||||||
var x = index % width;
|
|
||||||
var y = index / height;
|
|
||||||
var uv = new Vector2(x, y);
|
|
||||||
buffers[index] = GradientNoise(uv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private const int _WIDTH = 512;
|
private const int _WIDTH = 512;
|
||||||
private const int _HEIGHT = 512;
|
private const int _HEIGHT = 512;
|
||||||
private const int _LENGTH = _WIDTH * _HEIGHT;
|
private const int _LENGTH = _WIDTH * _HEIGHT;
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
using Misaki.HighPerformance.Unsafe.Collections;
|
using BenchmarkDotNet.Running;
|
||||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
using Misaki.HighPerformance.Test;
|
||||||
using System.Numerics;
|
|
||||||
|
|
||||||
unsafe
|
BenchmarkRunner.Run<CollectionBenchmark>();
|
||||||
{
|
|
||||||
Console.WriteLine(sizeof(UnsafeHashMap<int, float>));
|
|
||||||
Console.WriteLine(MemoryUtilities.AlignOf<UnsafeHashMap<int, float>>());
|
|
||||||
Console.WriteLine(1 << Math.Min(3, BitOperations.TrailingZeroCount(sizeof(UnsafeHashMap<int, float>))));
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#define UNSAFE_COLLECTION_CHECK
|
//#define UNSAFE_COLLECTION_CHECK
|
||||||
|
|
||||||
using Misaki.HighPerformance.Unsafe.Collections;
|
using Misaki.HighPerformance.Unsafe.Collections;
|
||||||
#if UNSAFE_COLLECTION_CHECK
|
#if UNSAFE_COLLECTION_CHECK
|
||||||
@@ -21,7 +21,7 @@ public static unsafe class AllocationManager
|
|||||||
private static Dictionary<IntPtr, MemoryLeakExceptionInfo> _allocated = null!;
|
private static Dictionary<IntPtr, MemoryLeakExceptionInfo> _allocated = null!;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private static readonly Lock _lock = new();
|
//private static readonly Lock _lock = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the AllocationManager with a specified initial size for the memory arena.
|
/// Initializes the AllocationManager with a specified initial size for the memory arena.
|
||||||
@@ -42,7 +42,7 @@ public static unsafe class AllocationManager
|
|||||||
_initialized = true;
|
_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
|
where T : unmanaged
|
||||||
{
|
{
|
||||||
if (allocationOption.HasFlag(AllocationOption.UnTracked))
|
if (allocationOption.HasFlag(AllocationOption.UnTracked))
|
||||||
@@ -55,45 +55,45 @@ public static unsafe class AllocationManager
|
|||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
lock (_lock)
|
//lock (_lock)
|
||||||
|
//{
|
||||||
|
T* buffer;
|
||||||
|
switch (allocator)
|
||||||
{
|
{
|
||||||
T* buffer;
|
case Allocator.Temp:
|
||||||
switch (allocator)
|
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:
|
case Allocator.Persistent:
|
||||||
var allocationSize = size * (nuint)sizeof(T);
|
var allocationSize = size * (nuint)sizeof(T);
|
||||||
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
|
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
|
||||||
|
|
||||||
#if UNSAFE_COLLECTION_CHECK
|
#if UNSAFE_COLLECTION_CHECK
|
||||||
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
|
_allocated[(IntPtr)buffer] = new MemoryLeakExceptionInfo
|
||||||
{
|
{
|
||||||
Size = allocationSize,
|
Size = allocationSize,
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
StackTrace = new StackTrace(true)
|
StackTrace = new StackTrace(true)
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (allocationOption.HasFlag(AllocationOption.Clear))
|
if (allocationOption.HasFlag(AllocationOption.Clear))
|
||||||
{
|
{
|
||||||
MemClear(buffer, allocationSize);
|
MemClear(buffer, allocationSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
|
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
|
||||||
}
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
where T : unmanaged
|
||||||
{
|
{
|
||||||
if (!_initialized)
|
if (!_initialized)
|
||||||
@@ -101,68 +101,62 @@ public static unsafe class AllocationManager
|
|||||||
throw new InvalidOperationException("The AllocationManager has not been initialized.");
|
throw new InvalidOperationException("The AllocationManager has not been initialized.");
|
||||||
}
|
}
|
||||||
|
|
||||||
lock (_lock)
|
//lock (_lock)
|
||||||
|
//{
|
||||||
|
T* newBuffer;
|
||||||
|
switch (allocator)
|
||||||
{
|
{
|
||||||
T* newBuffer;
|
case Allocator.Temp:
|
||||||
switch (allocator)
|
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:
|
case Allocator.Persistent:
|
||||||
var allocationSize = size * (nuint)sizeof(T);
|
var allocationSize = size * (nuint)sizeof(T);
|
||||||
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize);
|
newBuffer = (T*)AlignedRealloc(buffer, allocationSize, alignSize);
|
||||||
|
|
||||||
#if UNSAFE_COLLECTION_CHECK
|
#if UNSAFE_COLLECTION_CHECK
|
||||||
// If the allocation map can not find the old value, it means that it was a untracked allocation
|
// If the allocation map can not find the old value, it means that it was a untracked allocation
|
||||||
if (_allocated.Remove((IntPtr)buffer))
|
if (_allocated.Remove((IntPtr)buffer))
|
||||||
|
{
|
||||||
|
_allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
|
||||||
{
|
{
|
||||||
_allocated[(IntPtr)newBuffer] = new MemoryLeakExceptionInfo
|
Size = allocationSize,
|
||||||
{
|
|
||||||
Size = allocationSize,
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
StackTrace = new StackTrace(true)
|
StackTrace = new StackTrace(true)
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
|
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
|
||||||
}
|
|
||||||
|
|
||||||
return newBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
#if UNSAFE_COLLECTION_CHECK
|
||||||
_allocated.Remove((IntPtr)ptr);
|
_allocated.Remove((IntPtr)ptr);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the memory arena, optionally clearing the allocated memory.
|
/// Resets the memory arena, optionally clearing the allocated memory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
|
public static void Reset()
|
||||||
public static void Reset(bool clear = false)
|
|
||||||
{
|
{
|
||||||
if (!_initialized)
|
_arena.Reset();
|
||||||
{
|
|
||||||
throw new InvalidOperationException("The AllocationManager has not been initialized.");
|
|
||||||
}
|
|
||||||
|
|
||||||
_arena.Reset(clear);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -15,6 +15,16 @@ public unsafe struct Arena : IDisposable
|
|||||||
|
|
||||||
public Arena(uint size)
|
public Arena(uint size)
|
||||||
{
|
{
|
||||||
|
Initialize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Initialize(uint size)
|
||||||
|
{
|
||||||
|
if (_buffer != null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_buffer = (byte*)Malloc(size);
|
_buffer = (byte*)Malloc(size);
|
||||||
_size = size;
|
_size = size;
|
||||||
_offset = 0;
|
_offset = 0;
|
||||||
@@ -32,7 +42,10 @@ public unsafe struct Arena : IDisposable
|
|||||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||||
public void* Allocate(uint size, uint alignSize, AllocationOption allocationOption)
|
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);
|
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
|
||||||
if (offset + size > _size)
|
if (offset + size > _size)
|
||||||
@@ -56,13 +69,11 @@ public unsafe struct Arena : IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
|
/// <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>
|
/// <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 (_disposed)
|
||||||
|
|
||||||
if (clear)
|
|
||||||
{
|
{
|
||||||
MemClear(_buffer, _size);
|
throw new ObjectDisposedException(nameof(DynamicArena));
|
||||||
}
|
}
|
||||||
|
|
||||||
_offset = 0;
|
_offset = 0;
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
|
|
||||||
private ArenaNode* _root;
|
private ArenaNode* _root;
|
||||||
private ArenaNode* _current;
|
private ArenaNode* _current;
|
||||||
private readonly uint _initialSize;
|
private uint _initialSize;
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of DynamicArena with the specified initial size.
|
/// Initializes a new instance of DynamicArena with the specified initial size.
|
||||||
@@ -32,11 +31,25 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
_current = _root;
|
_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)
|
private bool CreateNewNode(uint size)
|
||||||
{
|
{
|
||||||
|
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
|
||||||
newNode->arena = new Arena(size);
|
newNode->arena = new Arena(size);
|
||||||
newNode->next = null;
|
newNode->next = null;
|
||||||
|
|
||||||
@@ -46,6 +59,7 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
Free(newNode);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,7 +73,10 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||||
public void* Allocate(uint size, uint alignSize, AllocationOption allocationType)
|
public void* Allocate(uint size, uint alignSize, AllocationOption allocationType)
|
||||||
{
|
{
|
||||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
if (_root == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
void* result = null;
|
void* result = null;
|
||||||
var current = _current;
|
var current = _current;
|
||||||
@@ -89,14 +106,12 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clear">If true, memory will be cleared during reset.</param>
|
/// <param name="clear">If true, memory will be cleared during reset.</param>
|
||||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
/// <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;
|
var current = _root;
|
||||||
while (current != null)
|
while (current != null)
|
||||||
{
|
{
|
||||||
current->arena.Reset(clear);
|
current->arena.Reset();
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +123,7 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_disposed)
|
if (_root == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -124,6 +139,5 @@ public unsafe struct DynamicArena : IDisposable
|
|||||||
|
|
||||||
_root = null;
|
_root = null;
|
||||||
_current = null;
|
_current = null;
|
||||||
_disposed = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
|||||||
public readonly ref T this[int index]
|
public readonly ref T this[int index]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
|
get => ref _buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly bool IsCreated
|
public readonly bool IsCreated
|
||||||
@@ -128,6 +128,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
|||||||
_allocator = Allocator.External;
|
_allocator = Allocator.External;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public void Resize(int newSize)
|
public void Resize(int newSize)
|
||||||
{
|
{
|
||||||
if (newSize == _count)
|
if (newSize == _count)
|
||||||
@@ -139,20 +140,28 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
|||||||
_count = newSize;
|
_count = newSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public readonly void Clear()
|
public readonly void Clear()
|
||||||
{
|
{
|
||||||
MemClear(_buffer, (nuint)(_count * sizeof(T)));
|
MemClear(_buffer, (nuint)(_count * sizeof(T)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public readonly void* GetUnsafePtr()
|
public readonly void* GetUnsafePtr()
|
||||||
{
|
{
|
||||||
return _buffer;
|
return _buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
if (!IsCreated)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
AllocationManager.Free(_buffer, _allocator);
|
AllocationManager.Free(_buffer, _allocator);
|
||||||
|
|
||||||
_buffer = null;
|
_buffer = null;
|
||||||
|
|||||||
@@ -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));
|
public IEnumerator<T> GetEnumerator() => new Enumerator((HashMapHelper<T>*)UnsafeUtilities.AddressOf(ref _hashMap));
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
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);
|
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, allocator, allocationOption);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,10 +118,12 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
|||||||
public readonly int Capacity => _array.Count;
|
public readonly int Capacity => _array.Count;
|
||||||
public readonly bool IsCreated => _array.IsCreated;
|
public readonly bool IsCreated => _array.IsCreated;
|
||||||
|
|
||||||
public readonly ref T this[int index]
|
public readonly T this[int index]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[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));
|
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||||
|
|||||||
@@ -70,10 +70,12 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
|
|||||||
public readonly int Capacity => _array.Count;
|
public readonly int Capacity => _array.Count;
|
||||||
public readonly bool IsCreated => _array.IsCreated;
|
public readonly bool IsCreated => _array.IsCreated;
|
||||||
|
|
||||||
public readonly ref T this[int index]
|
public readonly T this[int index]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[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));
|
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public class MemoryLeakException(params MemoryLeakExceptionInfo[] Infos) : Excep
|
|||||||
var frame = stackTrace.GetFrame(i);
|
var frame = stackTrace.GetFrame(i);
|
||||||
if (frame != null)
|
if (frame != null)
|
||||||
{
|
{
|
||||||
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Line: {frame.GetFileLineNumber()}");
|
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ public unsafe static class UnsafeCollectionExtensions
|
|||||||
/// <param name="source">Represents the source collection from which elements are copied.</param>
|
/// <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>
|
/// <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>
|
/// <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)
|
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="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>
|
/// <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>
|
/// <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)
|
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="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>
|
/// <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>
|
/// <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)
|
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="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>
|
/// <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>
|
/// <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)
|
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>
|
/// <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>
|
/// <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>
|
/// <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];
|
var array = new T[source.Count];
|
||||||
fixed (T* ptr = array)
|
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>
|
/// <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>
|
/// <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>
|
/// <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);
|
var list = new List<T>(source.Count);
|
||||||
fixed (T* ptr = list.ToArray())
|
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>
|
/// <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>
|
/// <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>
|
/// <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);
|
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="source">The collection to search for the specified value.</param>
|
||||||
/// <param name="value">The value to locate within the collection.</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>
|
/// <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++)
|
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="source">The collection being searched for the specified value.</param>
|
||||||
/// <param name="value">The value being searched for within the collection.</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>
|
/// <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);
|
source.IndexOf(value, out var index);
|
||||||
return index != -1;
|
return index != -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user