Skip to content

Instantly share code, notes, and snippets.

View mapping-benchmark.cs
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Entity, Model>();
}
}
public class MappingBenchmarks
{
View typesafety.cs
public static unsafe void TypeSafety()
{
int a = 1;
var ptr = Unsafe.AsPointer(ref a);
Unsafe.Write(ptr, 0);
var b = Unsafe.Read<bool>(ptr);
Console.WriteLine(b);
View unsafe-read.msil
.method public hidebysig static !!T Read<T>(void* source) cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 1
ldarg.0
ldobj !!T
ret
}
View unsafe-buffer-read-write.cs
public static unsafe byte[] WriteVectorBuffer(ref Vector3 vec)
{
Span<byte> buffer = stackalloc byte[sizeof(Vector3)];
fixed (byte* b = buffer)
Unsafe.Write(b, vec);
return buffer.ToArray();
}
View GetVectorBytesStackAlloc.cs
public static unsafe byte[] GetVectorBytes(ref Vector3 vec)
{
Span<byte> buffer = stackalloc byte[sizeof(Vector3)];
fixed (byte* b = buffer)
Marshal.StructureToPtr(vec, new IntPtr(b), false);
return buffer.ToArray();
}
View GetVectorBytes.cs
public static unsafe byte[] GetVectorBytes(Vector3 vec)
{
var buffer = new byte[sizeof(Vector3)];
fixed (byte* b = buffer)
{
Marshal.StructureToPtr(vec, new IntPtr(b), false);
return buffer;
}
}
View pointers.cs
static void Main(string[] args)
{
var vec = new Vector3 {X = 10, Y = 20, Z = 30};
Foo(ref vec);
}
public static unsafe void Foo(ref Vector3 vec)
{
fixed (Vector3* ptr = &vec)
{
View metrics.txt
# HELP request_duration_seconds The duration in seconds between the response to a request.
# TYPE request_duration_seconds histogram
request_duration_seconds_sum{status_code="200",method="GET"} 0.0385083
request_duration_seconds_count{status_code="200",method="GET"} 13
request_duration_seconds_bucket{status_code="200",method="GET",le="0.01"} 12
request_duration_seconds_bucket{status_code="200",method="GET",le="0.02"} 12
request_duration_seconds_bucket{status_code="200",method="GET",le="0.04"} 13
request_duration_seconds_bucket{status_code="200",method="GET",le="0.08"} 13
request_duration_seconds_bucket{status_code="200",method="GET",le="0.16"} 13
request_duration_seconds_bucket{status_code="200",method="GET",le="0.32"} 13
View Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<MetricReporter>();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other middleware components omitted for brevity
View ResponseMetricMiddleware.cs
public class ResponseMetricMiddleware
{
private readonly RequestDelegate _request;
public ResponseMetricMiddleware(RequestDelegate request)
{
_request = request ?? throw new ArgumentNullException(nameof(request));
}
public async Task Invoke(HttpContext httpContext, MetricReporter reporter)
You can’t perform that action at this time.