get the benchmark result, very POWERFULL

This commit is contained in:
Youmu Liu
2026-01-29 20:05:53 +08:00
parent 29bee99355
commit 01984dee28
10 changed files with 125 additions and 42 deletions
+40 -25
View File
@@ -32,10 +32,12 @@ using System.Runtime.InteropServices;
namespace StgSharp.Benchmark
{
[MemoryDiagnoser]
[InvocationCount(16)]
[InvocationCount(2048)]
public class HlsfAllocatorBench
{
private const uint Alignment = 64;
private const int AllocationCount = 2048;
private static readonly int[] AllocSizes = [
@@ -55,7 +57,7 @@ namespace StgSharp.Benchmark
[GlobalCleanup]
public void Cleanup()
{
_hlsfAllocator.Dispose();
_hlsfAllocator?.Dispose();
_hlsfLocalList.Clear();
_hlsfLocalList.TrimExcess();
_libcLocalList.Clear();
@@ -65,27 +67,7 @@ namespace StgSharp.Benchmark
[Benchmark]
public void Hlsf()
{
for (int i = 0; i < AllocationCount; i++)
{
lock (_hlsfAllocator)
{
HybridLayerSegregatedFitAllocationHandle allocate = _hlsfAllocator.Alloc(GetNextRandomSize());
_hlsfLocalList.Add(allocate);
}
}
for (int i = 0; i < _hlsfLocalList.Count; i++)
{
lock (_hlsfAllocator) {
_hlsfAllocator.Free(_hlsfLocalList[i]);
}
}
}
public void InvocationSetup()
{
_hlsfLocalList.Clear();
_libcLocalList.Clear();
RunHlsf(_hlsfAllocator, FreePolicy.NoCollect);
}
[IterationSetup]
@@ -98,9 +80,15 @@ namespace StgSharp.Benchmark
[Benchmark(Baseline = true)]
public unsafe void Libc()
{
_libcLocalList.Clear();
for (int i = 0; i < AllocationCount; i++)
{
void* allocate = NativeMemory.AlignedAlloc(GetNextRandomSize(), 64);
uint size = AlignSize(GetNextRandomSize());
void* allocate = NativeMemory.AlignedAlloc(size, Alignment);
if (allocate == null) {
throw new OutOfMemoryException("NativeMemory.AlignedAlloc returned null.");
}
_libcLocalList.Add((nint)allocate);
}
@@ -113,7 +101,15 @@ namespace StgSharp.Benchmark
public void Setup()
{
_random = new Random(42);
_hlsfAllocator = new HybridLayerSegregatedFitAllocator(GetArenaBytes(), align:64);
_hlsfAllocator = new HybridLayerSegregatedFitAllocator(GetArenaBytes(), align:(int)Alignment);
}
private static uint AlignSize(
uint size
)
{
uint mask = Alignment - 1;
return (size + mask) & ~mask;
}
private static nuint GetArenaBytes()
@@ -134,5 +130,24 @@ namespace StgSharp.Benchmark
private uint GetNextRandomSize() => (uint)AllocSizes[_random.Next(AllocSizes.Length)];
private void RunHlsf(
HybridLayerSegregatedFitAllocator allocator,
FreePolicy policy
)
{
_hlsfLocalList.Clear();
for (int i = 0; i < AllocationCount; i++)
{
uint size = AlignSize(GetNextRandomSize());
HybridLayerSegregatedFitAllocationHandle allocate = allocator.Alloc(size);
_hlsfLocalList.Add(allocate);
}
for (int i = 0; i < _hlsfLocalList.Count; i++) {
allocator.Free(_hlsfLocalList[i], policy);
}
}
}
}
+8 -1
View File
@@ -25,7 +25,11 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using StgSharp.Benchmark;
namespace StgSharp.Benchmark
@@ -37,7 +41,10 @@ namespace StgSharp.Benchmark
string[] args
)
{
BenchmarkRunner.Run<HlsfAllocatorBench>();
IConfig config = ManualConfig.Create(DefaultConfig.Instance)
.AddLogger(ConsoleLogger.Default)
.AddJob(Job.Default.WithToolchain(InProcessEmitToolchain.Instance));
BenchmarkRunner.Run<HlsfAllocatorBench>(config);
}
}