Meadow Getting Started

I have acquired a ProjectLab device with Meadow from Wilderness Labs. This post describes how to set up a development environment on Windows for such a device.

Installation

Install the latest .NET SDK. In my case at the time of writing, I am testing with .NET 8 Preview 3, hence that is installed my machine.

Next install the Meadow dotnet CLI. It is a dotnet tool, so we can install it as:

Find out more


Singletons with DI Container

A typical discussion topic about Dependency Injection (DI) Containers is creating singleton classes. Although many discussions come to the conclusion that the singleton pattern can be converted directly to singleton with a DI container, that is not entirely true.

First concern is that many DI containers support creating a child or scoped containers. Singleton registrations are instantiated with different rules based on the contract of the container. In that case it is possible that a child container creates its own singleton instance - but this is generally not the case. Today most containers differentiate scoped registrations from singletons to handle this case.

Second concern is that most DI containers require the user to implement the singleton type with a public constructor. This ensures that the DI container can invoke the constructor. However, it also implies that any user code may just as well instantiate further objects from the type. Even worse, the declaration of the type does not reflect its intended use.

The third concern is that a user can create multiple DI containers in an application. Each container can have its own singletons, as nothing restricts not to register the same (singleton) type with multiple containers.

Find out more


Method Size and CPU

Usually, assembly code size is good measure to have for performance optimizations. In this post I plan to gain an understanding why smaller assembly code size might be beneficial and how this affects the performance.

Problem

To answer this question, I prepare two methods to compare. How can I create two methods that do the same operations in essence but differ in code size that is executed?

Instead of writing 2 methods to compare, I decided to write ~2000 methods:

Find out more


The Best Way to ArgumentValidation

Introduction to Argument Validation

In .NET7 we can do 3 different style of argument validation against null arguments. In this post I will check which one is the most performant.

The first example is the poor man's validation. We check if the input parameter is null, and if so an ArgumentNullException object is instantiated and thrown. This style of argument validation has been around for an awfully long time in .NET.

public int CustomArgumentValidationImpl(Data? input)
{
    if (input == null)
        throw new ArgumentNullException(nameof(input));
    return input.Value;
}

Find out more


Understanding RuntimeHelpers.IsKnownConstant Method

In this post my goal is to gain understanding of the RuntimeHelpers.IsKnownConstant method. I have come across it in the Performance Improvements in .NET 7 blog post. There are two examples presented in the post:

  • bool StartsWith(char value)

  • Math.Round with MidpointRounding mode is specified

I will learn about JIT optimization including RuntimeHelpers.IsKnownConstant through a simple and slightly artificial example. A bool IsPalindromePosition(int position) method is part of a type which has a static property ReadOnlySpan<char> Text. The method returns true when the given position from the beginning of the string returns the same character as one from the end. For the sake of simplicity this method omits all input validation.

public static ReadOnlySpan<char> Text => "hellobolleh";

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public bool IsPalindromePosition(int position)
{
    if (int.IsOddInteger(Text.Length) && position == Text.Length / 2 + 1)
        return true;

    return Text[position] == Text[Text.Length - position - 1];
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public bool Work()
{
    var result = IsPalindromePosition(0);
    return result;
}

Find out more