mirror of
https://github.com/Facepunch/sbox-public.git
synced 2025-12-23 14:38:13 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
54 lines
1.1 KiB
C#
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 );
|
|
}
|
|
|
|
}
|