Changed the `AllocationManager` to use `ThreadLocal<DynamicArena>` for thread-specific memory allocation. Changed the `Initialize` method to directly validate `initialSize` and initialize the thread-local arena. Changed the `Allocate` and `Realloc` methods to utilize the thread-local arena for memory operations. Changed the `Free` method to remove unnecessary locking due to thread-local management. Added a new private method `EnsureInitialized` to verify the initialization of the thread-local arena. Added a public static method `ResetAll` to reset all thread-local arenas. Changed the `ResetCurrent` method to reset only the current thread's arena. Updated the `Dispose` method to clean up all thread-local arenas and clear the allocated dictionary. Uncommented the `UNSAFE_COLLECTION_CHECK` directive for enhanced debugging checks in `AllocationManager`. Changed `CollectionBenchmark` to call `AllocationManager.ResetCurrent()` after populating the `UnsafeArray<int>`.
55 lines
1.1 KiB
C#
55 lines
1.1 KiB
C#
using BenchmarkDotNet.Attributes;
|
|
using Misaki.HighPerformance.Unsafe.Buffer;
|
|
using Misaki.HighPerformance.Unsafe.Collections;
|
|
|
|
namespace Misaki.HighPerformance.Test;
|
|
|
|
[MemoryDiagnoser]
|
|
public unsafe class CollectionBenchmark
|
|
{
|
|
[Params(10, 100, 1000)]
|
|
public int count;
|
|
|
|
[GlobalSetup]
|
|
public void Setup()
|
|
{
|
|
AllocationManager.Initialize();
|
|
}
|
|
|
|
[Benchmark]
|
|
public void Array()
|
|
{
|
|
var array = new int[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
array[i] = i;
|
|
}
|
|
}
|
|
|
|
[Benchmark(Baseline = true)]
|
|
public void UnsafeArray()
|
|
{
|
|
var array = new UnsafeArray<int>(count, Allocator.Temp);
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
array[i] = i;
|
|
}
|
|
AllocationManager.ResetCurrent();
|
|
}
|
|
|
|
[Benchmark]
|
|
public void StackArray()
|
|
{
|
|
var array = stackalloc int[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
array[i] = i;
|
|
}
|
|
}
|
|
|
|
[GlobalCleanup]
|
|
public void Cleanup()
|
|
{
|
|
AllocationManager.Dispose();
|
|
}
|
|
} |