Files
sbox-public/engine/Benchmark/Benchmarks/MemoryAlloc.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

54 lines
1.1 KiB
C#

using BenchmarkDotNet.Attributes;
using System.Buffers;
using System.Runtime.InteropServices;
[MemoryDiagnoser]
public unsafe class MemoryAlloc
{
const int allocSize = 1024 * 32;
[Benchmark]
public void AllocHGlobal()
{
var ptr = Marshal.AllocHGlobal( allocSize );
Marshal.FreeHGlobal( ptr );
}
[Benchmark]
public void MemoryPool()
{
using var ptr = MemoryPool<byte>.Shared.Rent( allocSize );
using var ptrPinned = ptr.Memory.Pin();
}
[Benchmark]
public void ArrayPool()
{
var ptr = ArrayPool<byte>.Shared.Rent( allocSize );
ArrayPool<byte>.Shared.Return( ptr );
}
[Benchmark]
public void ArrayPoolPinned()
{
var ptr = ArrayPool<byte>.Shared.Rent( allocSize );
var ptrHandle = GCHandle.Alloc( ptr, GCHandleType.Pinned );
ptrHandle.Free();
ArrayPool<byte>.Shared.Return( ptr );
}
[Benchmark]
public void NativeMemoryAlloc()
{
var data = NativeMemory.Alloc( allocSize );
NativeMemory.Free( data );
}
[Benchmark]
public void NativeMemoryBlock()
{
using var ptr = Sandbox.NativeMemoryBlock.GetOrCreatePooled( allocSize );
}
}