Files
sbox-public/engine/Benchmark/Benchmarks/MemoryAlloc.cs
Lorenz Junglas 8f97c55540 Byte stream switch to ArrayPool for backing storage (#3714)
* Replace ByteStream replace NativeMemoryBlock with ArrayPool

* Remove ByteStream.Base, pin at call site instead

* Add Read Benchmarks to bytestream test

* Avoid pinning when reading from bytestream

* Remove NativeMemoryBlock

* Drop condition that never can be true
2026-01-09 13:14:48 +01:00

47 lines
951 B
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 );
}
}