Update memory allocation practices and clean up code

Changed the `ParallelNoiseBenchmark` class to use `AllocationOption.None` for `UnsafeArray<float>` instances.
Changed the `AllocationOption` enum to replace `UnInitialized` with `None` and clarified the `UnTracked` option.
Changed constructors for `UnsafeArray<T>`, `UnsafeList<T>`, `UnsafeQueue<T>`, and `UnsafeStack<T>` to use `AllocationOption.None`.
Changed the `HashMapHelper<TKey>` constructor to accept an `AllocationOption` parameter for improved flexibility.
Changed the `Allocate` method documentation in `Arena.cs` to clarify memory management.
Improved the `AllocationManager` class with a default arena size and updated allocation logic to handle new `AllocationOption` flags.
Removed the import statement for `BenchmarkDotNet.Running` in `Program.cs` and added new imports for `Misaki.HighPerformance.Unsafe.Collections` and `Misaki.HighPerformance.Unsafe.Services`.
Added a new `UnsafeArray<int>` in `Program.cs` and called `AllocationManager.Dispose()` to clean up resources.
This commit is contained in:
2025-04-03 16:59:32 +09:00
parent 1e00f4eb25
commit 3c555a9489
10 changed files with 72 additions and 52 deletions

View File

@@ -1,5 +1,4 @@
using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Buffer;
@@ -24,13 +23,14 @@ public unsafe struct Arena : IDisposable
/// <summary>
/// Allocates a block of memory of a specified size with a given alignment. Returns a pointer to the allocated
/// memory or null if allocation fails.
/// Must use <see cref="NativeMemory.AlignedFree"/> to free the memory.
/// You don't need to free the memory manually, it will be freed when the arena is disposed.
/// </summary>
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param>
/// <param name="alignSize">Defines the alignment requirement for the allocated memory.</param>
/// <param name="allocationOption">The option when allocating memory.</param>
/// <returns>A pointer to the allocated memory block or null if the allocation cannot be fulfilled.</returns>
/// <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 allocationOption)
{
ObjectDisposedException.ThrowIf(_disposed, this);
@@ -43,7 +43,7 @@ public unsafe struct Arena : IDisposable
_offset = offset + size;
var ptr = _buffer + offset;
if (allocationType == AllocationOption.Clear)
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(ptr, size);
}

View File

@@ -1,26 +1,27 @@
namespace Misaki.HighPerformance.Unsafe.Collections;
[Flags]
public enum AllocationOption : byte
{
/// <summary>
/// Allocator for uninitialized memory
/// </summary>
UnInitialized,
None = 0,
/// <summary>
/// Allocator for initialized memory.
/// </summary>
Clear,
Clear = 1 << 0,
/// <summary>
/// Allocator for untracked memory.
/// Allocator for untracked memory. It always allocates memory without using the allocation manager.
/// Always free it manually even if you use the <see cref="Allocator.Temp"/> allocator.
/// </summary>
/// <remarks>
/// Use this option carefully, as the allocation manager will not track the memory.
/// No warning will be given if the memory is not freed.
/// </summary>
UnTracked
/// </remarks>
UnTracked = 1 << 1,
}
public enum Allocator : byte
{
// Make the first allocator as invalid because we don't want to user create a defualt collection without passing any parameters
// Make the first allocator as invalid because we don't want to user create a default collection without passing any parameters
Invalid,
/// <summary>
/// Allocator for temporary allocations. Allocations are cleared after use.

View File

@@ -93,7 +93,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <param name="allocator">Specifies the allocator to use for memory allocation, which determines the memory management strategy.</param>
/// <param name="allocationOption">Determines how the allocated memory should be initialized, either uninitialized or cleared.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnsafeArray(int count, Allocator allocator, AllocationOption allocationOption = AllocationOption.UnInitialized)
public UnsafeArray(int count, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
if (count <= 0)
{
@@ -102,6 +102,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_buffer = AllocationManager.Allocate<T>((uint)count, (uint)AlignOf<T>(), allocator, allocationOption);
_count = count;
_allocator = allocator;
if (allocationOption == AllocationOption.Clear)
{

View File

@@ -129,7 +129,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
public ParallelWriter AsParallelWriter() => new((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
public UnsafeList(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.UnInitialized)
public UnsafeList(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
{
_array = new UnsafeArray<T>(capacity, allocator, allocationType);
}

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -80,7 +79,7 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.UnInitialized)
public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
{
_array = new UnsafeArray<T>(capacity, allocator, allocationType);
}

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -24,7 +23,7 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
return GetEnumerator();
}
public UnsafeStack(int initialSize, Allocator allocator, AllocationOption allocationOption = AllocationOption.UnInitialized)
public UnsafeStack(int initialSize, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
_array = new UnsafeArray<T>(initialSize, allocator, allocationOption);
}

View File

@@ -74,7 +74,9 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
private readonly int _sizeOfTValue;
private readonly int _log2MinGrowth;
private readonly Allocator _allocator;
private readonly AllocationOption _allocationOption;
public const int MINIMAL_CAPACITY = 64;
@@ -126,7 +128,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return totalSize;
}
public HashMapHelper(int capacity, int sizeOfTValue, uint minGrowth, Allocator allocator)
public HashMapHelper(int capacity, int sizeOfTValue, uint minGrowth, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
if (capacity <= 0)
{
@@ -143,7 +145,9 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_sizeOfTValue = sizeOfTValue;
_log2MinGrowth = BitOperations.Log2(minGrowth);
_allocator = allocator;
_allocationOption = allocationOption;
var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue,
out var keyOffset, out var nextOffset, out var bucketOffset);
@@ -182,7 +186,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
{
var alignSize = sizeof(TKey) > sizeof(int) ? AlignOf<TKey>() : AlignOf<int>();
_buffer = AllocationManager.Allocate<byte>((uint)totalSize, (uint)alignSize, _allocator, AllocationOption.UnInitialized);
_buffer = AllocationManager.Allocate<byte>((uint)totalSize, (uint)alignSize, _allocator, _allocationOption);
_keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset);
@@ -411,7 +415,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
internal UnsafeArray<TKey> GetKeyArray(Allocator allocator)
{
var result = new UnsafeArray<TKey>(_count, allocator, AllocationOption.UnInitialized);
var result = new UnsafeArray<TKey>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
{
@@ -430,7 +434,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
internal UnsafeArray<TValue> GetValueArray<TValue>(Allocator allocator)
where TValue : unmanaged
{
var result = new UnsafeArray<TValue>(_count, allocator, AllocationOption.UnInitialized);
var result = new UnsafeArray<TValue>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; ++i)
{
@@ -449,7 +453,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays<TValue>(Allocator allocator)
where TValue : unmanaged
{
var result = new UnsafeArray<KeyValuePair<TKey, TValue>>(_count, allocator, AllocationOption.UnInitialized);
var result = new UnsafeArray<KeyValuePair<TKey, TValue>>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
{

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.Unsafe.Buffer;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Services;
@@ -12,6 +11,8 @@ public static unsafe class AllocationManager
public readonly nuint size = size;
}
private const uint _DEFAULT_ARENA_SIZE = 512 * 1024; // 512 KB
private static DynamicArena _arena;
private static bool _initialized;
@@ -19,20 +20,11 @@ public static unsafe class AllocationManager
private static readonly Lock _lock = new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void VerifyInitialization()
{
if (!_initialized)
{
throw new InvalidOperationException("The AllocationManager has not been initialized.");
}
}
/// <summary>
/// Initializes the AllocationManager with a specified initial size for the memory arena.
/// </summary>
/// <param name="initialSize">The initial size in bytes for the memory arena.</param>
public static void Initialize(uint initialSize)
public static void Initialize(uint initialSize = _DEFAULT_ARENA_SIZE)
{
if (_initialized || initialSize <= 0)
{
@@ -40,7 +32,7 @@ public static unsafe class AllocationManager
}
_arena = new DynamicArena(initialSize);
_allocated = new UnsafeQueue<AllocationInfo>(16, Allocator.Persistent);
_allocated = new UnsafeQueue<AllocationInfo>(32, Allocator.Persistent, AllocationOption.UnTracked);
_initialized = true;
}
@@ -48,29 +40,41 @@ public static unsafe class AllocationManager
internal static T* Allocate<T>(uint size, uint alignSize, Allocator allocator, AllocationOption allocationOption)
where T : unmanaged
{
if (allocationOption == AllocationOption.UnTracked)
if (allocationOption.HasFlag(AllocationOption.UnTracked))
{
return (T*)AlignedAlloc(size, alignSize);
}
VerifyInitialization();
if (!_initialized)
{
Initialize();
}
lock (_lock)
{
T* buffer;
switch (allocator)
{
case Allocator.Temp:
return (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, allocationOption);
buffer = (T*)_arena.Allocate(size * (uint)sizeof(T), alignSize, allocationOption);
break;
case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T);
var buffer = (T*)AlignedAlloc(allocationSize, alignSize);
buffer = (T*)AlignedAlloc(allocationSize, alignSize);
_allocated.Enqueue(new AllocationInfo(buffer, allocationSize));
return buffer;
break;
default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type.");
}
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(buffer, size * (uint)sizeof(T));
}
return buffer;
}
}
@@ -91,7 +95,11 @@ public static unsafe class AllocationManager
/// <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)
{
VerifyInitialization();
if (!_initialized)
{
throw new InvalidOperationException("The AllocationManager has not been initialized.");
}
_arena.Reset(clear);
}
@@ -101,20 +109,25 @@ public static unsafe class AllocationManager
/// <exception cref="InvalidOperationException">Thrown if there are still allocated buffers that have not been freed.</exception>
public static void Dispose()
{
if (!_initialized)
{
return;
}
_arena.Dispose();
nuint unfreedBytes = 0u;
nuint unfreeBytes = 0u;
while (_allocated.TryDequeue(out var allocationInfo))
{
unfreedBytes += allocationInfo.size;
unfreeBytes += allocationInfo.size;
AlignedFree(allocationInfo.ptr);
}
_allocated.Dispose();
if (unfreedBytes > 0u)
if (unfreeBytes > 0u)
{
throw new InvalidOperationException($"There are still {unfreedBytes} bytes allocated buffers. Please free them before disposing.");
throw new InvalidOperationException($"There are still {unfreeBytes} bytes allocated buffers are not freed yet. Please free them before disposing.");
}
}
}