Heap Allocation free Parsing: structs

I have been working on a library RapideFix to parse fix messages. One of the challenges is to parse a message into a struct with the least amount of allocations possible. That means to allocate only for the references (if any) that are part of the struct's fields.

For the sake of this post, I will use a very simple struct with 2 integer properties and the parser method will set values to them. However, the code should be dynamic, so the user can replace the struct to any other custom one.

Starting Point

So, let's start with a sample code, which is incorrect after all, but it demonstrates what I want to achieve (if it worked correctly).So what I want to do is that given a string with two integers separated by commas "20,19,", and the parser should set the integer values on a struct, Point:

Find out more


WDS Setup for my Router

This is a rather unusual post. It is not going to deal with software development .net or C#, but it solves a networking issue.

Problem

My landline Internet Service Provider has increased the cost of the service by 30% while given no additional value. As I had unlimited mobile Internet connection already, so I decided to cancel my landline and depend on my mobile connection only. This was a smart move from my point of view, because I spend less-and-less time in the apartment in question with the landline. For this though it meant I need to run my Android mobile phone in hotspot mode, and all other devices would connect to it.

Solution

Find out more


NDC London 2019

After every great conference, I create a list of my favorite talks. This year in January I had an opportunity to attend to NDC the first time in person.

As videos have been uploaded to YouTube, I recommend the following sessions.

C#

Much Ado about Nothing: A C# play in two acts - Act 1- Mads Torgersen, Bill Wagnerhttps://www.youtube.com/watch?v=XGn15woZQ4o

Find out more


Creating C# Analyzers - Is EASY

Creating C# analyzers with the help of Roslyn is very easy. At first it seems a huge learning curve needed because of Roslyn and the complexity of compilers, but in reality, it is much simpler than expected.

There is one more advantage of creating analyzers. It give a perfect environment to practice Test Driven Development (TDD).

Before beginning we need to make sure, that the analyzer extensions are installed to Visual Studio, otherwise we will not have templates and Syntax Visualizer window. To install the SDK simply enable .NET Compiler Platform SDK feature in Visual Studio.

In this post I will show how to create an analyzer, which checks if an if statement or a conditional operator uses equality comparison operator to compare an operand with null. If that is the case, the analyzer will suggest a to replace the comparison with is pattern matching with nulls, which syntax is available with the latest C# version.

Find out more


ValueTask<T> versus Task<T>

Introduction

ValueTask has been recently introduced to C# to comprehend use-cases where allocation of a Task would be a problem. When it comes to deciding whether to use Task or ValueTask it is always suggested to measure the given application code path.

When a Task is awaited a state machine is being generated by the compiler. This state machine (a struct) executes through the asynchronous states of the method, also it preserves the internal state of the method. When we hit a state where we need to await another task, this struct will become boxed, so the state can be preserved between different threads that might execute different states of the method. When we execute synchronously though, no boxing would be required. However, the Task we return still needs to be allocated. The class library tries its best to use cached tasks, still we might hit a case where a new Task is being allocated. To avoid this allocation we can use ValueTask. Here are some remarks of the ValueTask documentation:

A method may return an instance of this value type when it's likely that the result of its operation will be available synchronously, and when it's expected to be invoked so frequently that the cost of allocating a new Task for each call will be prohibitive.

Find out more