-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCustomAllocatorBenchmarks.cs
More file actions
57 lines (47 loc) · 1.81 KB
/
CustomAllocatorBenchmarks.cs
File metadata and controls
57 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using BenchmarkDotNet.Attributes;
using LibDeflate.Imports;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace LibDeflate.Benchmarks;
[MemoryDiagnoser]
[SimpleJob]
public class CustomAllocatorBenchmarks
{
[GlobalSetup(Target = nameof(CompressorAllocCustom))]
public void SetCustomAllocator()
{
Console.WriteLine("Custom Allocator: set");
CustomMemoryAllocator.libdeflate_set_memory_allocator(malloc, free);
static nint malloc(nuint len) => Marshal.AllocHGlobal((nint)len);
static void free(nint alloc) => Marshal.FreeHGlobal(alloc);
}
[GlobalSetup(Target = nameof(CompressorAllocCustomUnsafe))]
public unsafe void SetCustomAllocatorUnsafe()
{
Console.WriteLine("Custom Unsafe Allocator: set");
CustomMemoryAllocator.libdeflate_set_memory_allocator_unsafe(&malloc_unsafe, &free_unsafe);
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
static unsafe void* malloc_unsafe(nuint len) => NativeMemory.Alloc(len);
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
static unsafe void free_unsafe(void* alloc) => NativeMemory.Free(alloc);
}
[Benchmark(Baseline = true)]
public void CompressorAlloc()
{
var compressor = Compression.libdeflate_alloc_compressor(0);
Compression.libdeflate_free_compressor(compressor);
}
[Benchmark]
public void CompressorAllocCustom()
{
var compressor = Compression.libdeflate_alloc_compressor(0);
Compression.libdeflate_free_compressor(compressor);
}
[Benchmark]
public void CompressorAllocCustomUnsafe()
{
var compressor = Compression.libdeflate_alloc_compressor(0);
Compression.libdeflate_free_compressor(compressor);
}
}