Skip to content

Instantly share code, notes, and snippets.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmarks>();
[SimpleJob, MemoryDiagnoser, DisassemblyDiagnoser]
public class Benchmarks
{
@ladeak
ladeak / source-gen-json-aspnetcore.md
Created January 26, 2024 21:34
Source Generated JSON Serialization using fast path in ASP.NET Core

Source Generated JSON Serialization using fast path in ASP.NET Core

In this post I explore how ASP.NET Core works together with the source generated JSON serializer. In .NET 8 the streaming JSON serialization can be enabled to use serialization-optimization mode with ASP.NET Core. However, using the default settings does not enable this fast-path. In this post I will explore how to enable the fast-path serialization by exploring the inner workings of JsonTypeInfo.cs. For this post I use .NET 8 with the corresponding ASP.NET Core release.

The .NET 8 runtime comes with three built-in JSON serializer modes:

  • Reflection
  • Source generation (Metadata-based mode)
  • Source generation (Serialization-optimization mode)
@ladeak
ladeak / meadow-bme688.md
Created September 16, 2023 17:29
Building an IAQ device with Meadow

Building an IAQ device with Meadow

Indoor air quality (IAQ) is increasingly important for our health, productivity and well-being. The industrialized world suffers from air pollution, and buildings are no exceptions. A room's air might be filled with pollutants such as bacteria, carbon monoxide or carbon dioxide.

Meadow is a microcontroller that can run C# code. It is designed by Wilderness Labs. Meadow can be attached to a Project Lab, which is a functional IoT prototyping platform. It has numerous sensors built-in, buttons, a screen and a wide variety of connectors.

One particular sensor on the Project Lab board is the BME688 manufactured by Bosch. This sensor is interesting as besides the regular temperature, pressure, humidity, it can measure *gas

@ladeak
ladeak / CopyFractional.cs
Last active September 8, 2023 06:07
Fractional
public record struct CopyFractional : ISpanFormattable
{
private byte _fractions128;
public required uint Integer { get; init; }
public required byte Fractions128
{
get => _fractions128;
init
{

Exploring Inline Arrays

Inline arrays have been introduced as new feature in C# 12. As the feature is preview at the time of writing, it may change before the .NET team releases the final version of C#.

A type attributed with inline array can be used to represent a fixed-size, contiguous sequence of primitives. The runtime and language provide type and overrun safe indexing operations to the underlying primitives.

At the time of writing one can define an inline array by applying the [InlineArray] attribute on a struct, that has a single field.

[System.Runtime.CompilerServices.InlineArray(8)]
@ladeak
ladeak / ChainedArrayBufferWriter.cs
Last active July 30, 2024 14:04
ArrayBufferWriters
using System.Buffers;
using Microsoft.Extensions.ObjectPool;
public class ChainedPolicy : IPooledObjectPolicy<ChainedArrayBufferWriter<byte>>
{
public ChainedArrayBufferWriter<byte> Create()
{
return new ChainedArrayBufferWriter<byte>();
}
@ladeak
ladeak / clocks.md
Last active June 29, 2022 20:07
SystemClock and StubClock

Thank you for the longer answer on my question. I choose to come back here with a longer reply because it feels a more appropriate form. After reading your post I still feel using a StubClock with constant value is my 'default' choice. I write this reply so that myself also understand why that would be the case.

Context

  • You likely have more experience working with DateTime
  • Maybe I have been exposed to a different type of business processes using DateTime that implies different usage
  • Maybe there is a pendulum swing waiting for me to happen

Determinism

@ladeak
ladeak / PerformanceResults.md
Last active January 22, 2022 15:54
Regex and Faster

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000 Intel Core i5-1035G4 CPU 1.10GHz, 1 CPU, 8 logical and 4 physical cores .NET SDK=7.0.100-alpha.1.22053.1 [Host] : .NET 7.0.0 (7.0.21.63101), X64 RyuJIT DefaultJob : .NET 7.0.0 (7.0.21.63101), X64 RyuJIT

Method Mean Error StdDev Gen 0 Allocated
Solution0 7,494.9 ns 298.81 ns 866.91 ns 3.6850 11 KB
@ladeak
ladeak / Font
Last active June 19, 2024 21:51
Terminal
https://www.nerdfonts.com/font-downloads
CaskaydiaCode Nerd Font
winget install JanDeDobbeleer.OhMyPosh -s winget
@ladeak
ladeak / string-interpolation-stringbuilder-2.md
Last active August 27, 2021 09:54
string-interpolation-stringbuilder.md

String Interpolation and StringBuilder in .NET6

In a previous post I have looked into what are the performance characteristics of creating interpolated string in .NET 5 and early previews of .NET 6. With .NET 6 preview 7 or better a new approach is used by the C# compiler with support from the BCL to build interpolated strings.

Fortunately the new way provides a faster approach for the most convenient ways for assembling strings, however it makes my previous post completely obsolete.

For details about the new way DefaultInterpolatedStringHandler builds strings, read: https://devblogs.microsoft.com/dotnet/string-interpolation-in-c-10-and-net-6/

Re-running the benchmarks from the previous post, shows much better memory usage: