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

@@ -61,7 +61,7 @@ public class ParallelNoiseBenchmark
[Benchmark] [Benchmark]
public static void JobSystem() public static void JobSystem()
{ {
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.UnInitialized); using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
var job = new NoiseJob() var job = new NoiseJob()
{ {
buffers = buffers, buffers = buffers,
@@ -76,7 +76,7 @@ public class ParallelNoiseBenchmark
[Benchmark] [Benchmark]
public static void ParallelFor() public static void ParallelFor()
{ {
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.UnInitialized); using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
Parallel.For(0, _LENGTH, i => Parallel.For(0, _LENGTH, i =>
{ {
@@ -90,7 +90,7 @@ public class ParallelNoiseBenchmark
[Benchmark] [Benchmark]
public static void For() public static void For()
{ {
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.UnInitialized); using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
for (var i = 0; i < _LENGTH; i++) for (var i = 0; i < _LENGTH; i++)
{ {
var x = i % _WIDTH; var x = i % _WIDTH;

View File

@@ -1,4 +1,7 @@
using BenchmarkDotNet.Running; using Misaki.HighPerformance.Unsafe.Collections;
using Misaki.HighPerformance.Test; using Misaki.HighPerformance.Unsafe.Services;
BenchmarkRunner.Run<CollectionBenchmark>(); var unfreeArray = new UnsafeArray<int>(10, Allocator.Persistent);
unfreeArray.Dispose();
AllocationManager.Dispose();

View File

@@ -1,5 +1,4 @@
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Buffer; namespace Misaki.HighPerformance.Unsafe.Buffer;
@@ -24,13 +23,14 @@ public unsafe struct Arena : IDisposable
/// <summary> /// <summary>
/// Allocates a block of memory of a specified size with a given alignment. Returns a pointer to the allocated /// 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. /// 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> /// </summary>
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param> /// <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="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> /// <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> /// <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); ObjectDisposedException.ThrowIf(_disposed, this);
@@ -43,7 +43,7 @@ public unsafe struct Arena : IDisposable
_offset = offset + size; _offset = offset + size;
var ptr = _buffer + offset; var ptr = _buffer + offset;
if (allocationType == AllocationOption.Clear) if (allocationOption.HasFlag(AllocationOption.Clear))
{ {
MemClear(ptr, size); MemClear(ptr, size);
} }

View File

@@ -1,26 +1,27 @@
namespace Misaki.HighPerformance.Unsafe.Collections; namespace Misaki.HighPerformance.Unsafe.Collections;
[Flags]
public enum AllocationOption : byte public enum AllocationOption : byte
{ {
/// <summary> None = 0,
/// Allocator for uninitialized memory
/// </summary>
UnInitialized,
/// <summary> /// <summary>
/// Allocator for initialized memory. /// Allocator for initialized memory.
/// </summary> /// </summary>
Clear, Clear = 1 << 0,
/// <summary> /// <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. /// Use this option carefully, as the allocation manager will not track the memory.
/// No warning will be given if the memory is not freed. /// No warning will be given if the memory is not freed.
/// </summary> /// </remarks>
UnTracked UnTracked = 1 << 1,
} }
public enum Allocator : byte 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, Invalid,
/// <summary> /// <summary>
/// Allocator for temporary allocations. Allocations are cleared after use. /// 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="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> /// <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> /// <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) 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); _buffer = AllocationManager.Allocate<T>((uint)count, (uint)AlignOf<T>(), allocator, allocationOption);
_count = count; _count = count;
_allocator = allocator;
if (allocationOption == AllocationOption.Clear) 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 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); _array = new UnsafeArray<T>(capacity, allocator, allocationType);
} }

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts; using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers; using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections; using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections; 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)); public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeQueue<T>*)UnsafeUtilities.AddressOf(ref this));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 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); _array = new UnsafeArray<T>(capacity, allocator, allocationType);
} }

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts; using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers; using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections; using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections; namespace Misaki.HighPerformance.Unsafe.Collections;
@@ -24,7 +23,7 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
return GetEnumerator(); 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); _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 _sizeOfTValue;
private readonly int _log2MinGrowth; private readonly int _log2MinGrowth;
private readonly Allocator _allocator; private readonly Allocator _allocator;
private readonly AllocationOption _allocationOption;
public const int MINIMAL_CAPACITY = 64; public const int MINIMAL_CAPACITY = 64;
@@ -126,7 +128,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return totalSize; 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) if (capacity <= 0)
{ {
@@ -143,7 +145,9 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_sizeOfTValue = sizeOfTValue; _sizeOfTValue = sizeOfTValue;
_log2MinGrowth = BitOperations.Log2(minGrowth); _log2MinGrowth = BitOperations.Log2(minGrowth);
_allocator = allocator; _allocator = allocator;
_allocationOption = allocationOption;
var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue, var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue,
out var keyOffset, out var nextOffset, out var bucketOffset); 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>(); 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); _keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset); _next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset); _buckets = (int*)(_buffer + bucketOffset);
@@ -411,7 +415,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
internal UnsafeArray<TKey> GetKeyArray(Allocator allocator) 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++) 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) internal UnsafeArray<TValue> GetValueArray<TValue>(Allocator allocator)
where TValue : unmanaged 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) 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) public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays<TValue>(Allocator allocator)
where TValue : unmanaged 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++) 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.Buffer;
using Misaki.HighPerformance.Unsafe.Collections; using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Services; namespace Misaki.HighPerformance.Unsafe.Services;
@@ -12,6 +11,8 @@ public static unsafe class AllocationManager
public readonly nuint size = size; public readonly nuint size = size;
} }
private const uint _DEFAULT_ARENA_SIZE = 512 * 1024; // 512 KB
private static DynamicArena _arena; private static DynamicArena _arena;
private static bool _initialized; private static bool _initialized;
@@ -19,20 +20,11 @@ public static unsafe class AllocationManager
private static readonly Lock _lock = new(); 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> /// <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.
/// </summary> /// </summary>
/// <param name="initialSize">The initial size in bytes for the memory arena.</param> /// <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) if (_initialized || initialSize <= 0)
{ {
@@ -40,7 +32,7 @@ public static unsafe class AllocationManager
} }
_arena = new DynamicArena(initialSize); _arena = new DynamicArena(initialSize);
_allocated = new UnsafeQueue<AllocationInfo>(16, Allocator.Persistent); _allocated = new UnsafeQueue<AllocationInfo>(32, Allocator.Persistent, AllocationOption.UnTracked);
_initialized = true; _initialized = true;
} }
@@ -48,29 +40,41 @@ public static unsafe class AllocationManager
internal static T* Allocate<T>(uint size, uint alignSize, Allocator allocator, AllocationOption allocationOption) internal static T* Allocate<T>(uint size, uint alignSize, Allocator allocator, AllocationOption allocationOption)
where T : unmanaged where T : unmanaged
{ {
if (allocationOption == AllocationOption.UnTracked) if (allocationOption.HasFlag(AllocationOption.UnTracked))
{ {
return (T*)AlignedAlloc(size, alignSize); return (T*)AlignedAlloc(size, alignSize);
} }
VerifyInitialization(); if (!_initialized)
{
Initialize();
}
lock (_lock) lock (_lock)
{ {
T* buffer;
switch (allocator) switch (allocator)
{ {
case Allocator.Temp: 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: case Allocator.Persistent:
var allocationSize = size * (nuint)sizeof(T); var allocationSize = size * (nuint)sizeof(T);
var buffer = (T*)AlignedAlloc(allocationSize, alignSize); buffer = (T*)AlignedAlloc(allocationSize, alignSize);
_allocated.Enqueue(new AllocationInfo(buffer, allocationSize)); _allocated.Enqueue(new AllocationInfo(buffer, allocationSize));
return buffer; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(allocator), "Invalid allocator type."); 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> /// <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(bool clear = false)
{ {
VerifyInitialization(); if (!_initialized)
{
throw new InvalidOperationException("The AllocationManager has not been initialized.");
}
_arena.Reset(clear); _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> /// <exception cref="InvalidOperationException">Thrown if there are still allocated buffers that have not been freed.</exception>
public static void Dispose() public static void Dispose()
{ {
if (!_initialized)
{
return;
}
_arena.Dispose(); _arena.Dispose();
nuint unfreedBytes = 0u; nuint unfreeBytes = 0u;
while (_allocated.TryDequeue(out var allocationInfo)) while (_allocated.TryDequeue(out var allocationInfo))
{ {
unfreedBytes += allocationInfo.size; unfreeBytes += allocationInfo.size;
AlignedFree(allocationInfo.ptr); AlignedFree(allocationInfo.ptr);
} }
_allocated.Dispose(); _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.");
} }
} }
} }