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

@@ -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;