Laszlo

Hello, I am Laszlo

Software-Enginner, .NET developer

Contact Me

Async Task Closures

In .NET async methods get compiled to an async state machine. When awaiting a method call returning a Task the state of the current method is captured by a compiler generated value type, that also implements IAsyncStateMachine.

In this blog post I use .NET 9 to explore some internals of this behavior.

Capturing Structs

I have recently encountered one such async method in production code. The method received a few input parameters, created a struct instance populating its properties with the input parameters. Then serialized the struct instance to string and sent an HTTP POST request using HttpClient while awaiting the result.

Find out more »


SIMD Gather and Scatter with Contains All ASCII

Introduction

One of the most difficult problems with SIMD is handling non-contiguous memory access. To address this challenge AVX-512 adds gather and scatter instructions to load and store memory in an array at non-adjacent indexes. These instructions enable a whole new set of algorithms to be vectorized using SIMD operations.

Gather is a single instruction that loads data from non-adjacent indexes of an array into a Vector register.Scatter is a single instruction that stores data at non-adjacent indexes to an array from a Vector register.

Both instructions have a source/destination register parameter, a reference to an array parameter, and another vector parameter containing the indexes for each lane to be loaded or stored.

Find out more »


Diagnostics Allocations in ASP.NET Core

Object Allocations

ASP.NET Core's Kestrel is optimized for high performance and scale. To achieve high performance, it reduces heap allocations by either pooling large objects or by allocating structs on the stack. By reducing allocations, the GC has less work to do. As pooled objects get promoted to Gen2 generation, the most common collections (Gen0 and Gen1) become cheaper as they contain fewer objects to handle. However, pooling is not entirely free:

  • it increases the Gen2 size and its corresponding collections

  • cross-generation references may require tracking references from Gen2 regions pointing to lower generation regions (for example, when a pooled object contains a reference to a newly allocated object).

One example is type Http2Stream (or Http3Stream), which corresponds to a request-response pair in a connection. As such objects are large, they are typically pooled. These objects may have a reference to the corresponding HttpContext, which should be also pooled, otherwise it incurs an allocation or a cross-generation reference.

Find out more »


.NET Dump Cheat Sheet

This post collects some useful commands diagnosing memory dumps. This document is written with a sample apps and tools targeting .NET 9. Some tools still name regions as segments.

dotnet-dump

Use dotnet-dump to collect a memory dump.Install using dotnet tool install dotnet-dump -g

It can do full dump/mini dump, etc.

Find out more »


Scaling ASP.NET Core Minimal API Responses

I have been running an ASP.NET Core application on Raspberry Pi Zero 2W. In an early performance test, I have deployed an application with two endpoints: / and /data. These endpoints return a simple string response as shown below.

app.MapGet("/", () =>
{
    return "Hello World";
});

app.MapGet("/data", (HttpContext ctx) =>
{
    ctx.Response.StatusCode = 200;
    var buffer = ctx.Response.BodyWriter.GetSpan(11);
    "Hello World"u8.CopyTo(buffer);
    ctx.Response.BodyWriter.Advance(11);
});

The application is compiled for .NET 9, without enabling native AOT. I have measured the performance of these APIs using CHttp tool.

CHttp is a simple tool to test performance of HTTP endpoints.

Find out more »