Skip to content

Instantly share code, notes, and snippets.

@tonyredondo
tonyredondo / Defer.cs
Created March 8, 2025 23:57
Performant Go defer keyword in C#
using System.Runtime.CompilerServices;
/// <summary>
/// Represents a scope that defers the execution of actions until disposal.
/// Actions are executed in last-in, first-out (LIFO) order when the scope ends.
/// </summary>
public ref struct DeferScope64
{
private const uint Capacity = 64;
private Buffer64 _buffer;
## Test02: Struct factory over Delegate factory
```
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.17134.523 (1803/April2018Update/Redstone4)
Intel Core i7-4770R CPU 3.20GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.2.101
[Host] : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT
Core : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT
Job=Core Runtime=Core
using BenchmarkDotNet.Attributes;
using System;
using System.Reflection;
namespace ConsoleBenchTest
{
class Program
{
static void Main(string[] args)
{
Avg Req/Sec Std. Dev. % Delta
No Coverage 73888.35 1438.84 100% -
With Coverage Disabled 73236.34 2258.05 99.12% -0.88%
With Coverage Enabled 64465.51 1830.63 88.02% -11.98%
Avg Req/Sec Std. Dev. % Delta
No Coverage 48459.94 388.34 100% -
With Coverage Disabled 48456.47 376.71 99.99% -0.01%
With Coverage Enabled 46529.14 447.39 96.02% -3.98%
Avg Req/Sec Std. Dev. % Delta
No Coverage 48072.39 472.12 100% -
With Coverage Disabled 47939.73 422.92 99.72% -0.28%
With Coverage Enabled 41821.98 711.15 87.24% -12.76%
@tonyredondo
tonyredondo / CB-Coverage-Perf01.md
Last active December 18, 2019 14:33
Results in a 13 inch MacBook Pro of late 2014
Avg Req/Sec Std. Dev. % Delta
No Coverage 21665.11 385.31 100% -
With Coverage Disabled 20982.86 680.18 96.85% -3.15%
With Coverage Enabled 16990.16 242.84 78.42% -21.58%