.NET internals

Inlining of intrinsic functions in .NET 5

For the last months, I was focused on developing Cosette chess engine, toying with a very wide spectrum of algorithms and language optimizations. The second one was especially interesting because of .NET 5 release, which brings a few interesting speed improvements (I will try to write something about them in the context of the engine soon). In this article, I will show an interesting behavior of inlining intrinsic function in some specific case which led to the dropping of performance in the chess engine.

Mystery of Random class in .NET Framework and .NET Core

Random class is one of the most used parts of the .NET library, which contains a few methods to generate pseudo-random numbers. They are extremely simple to use, but even with this, there are still some traps waiting for a programmer. In this article, I will focus on differences in implementation of this class between .NET Framework and .NET Core, especially seed generation which sometimes leads to interesting bugs.

Magic behind closures

Closure is a very important concept related to anonymous functions and lambdas. They allow a programmer to use local variables of the parent method inside a body of the inline function and then execute it at any time. The question is, how the C# compiler saves these local variables and how are they restored later - let’s check it.

GetHashCode inside CLR: Value types

In the previous article, we talked a bit about hash codes and how they are implemented for reference types - it turned out that it’s just a simple multiplication of thread ID and a random number. Today, we will do the same thing for value types, which are far more complex due to their representation in memory. In the end, I will show a small benchmark to prove that every struct defined by the programmer should override GetHashCode method. Time to dig into CLR source code!

GetHashCode inside CLR: Reference types

GetHashCode, a part of the Object class, is one of the key method present in every class instance. Its main purpose is to calculate and return a number associated with the specified object, which will be used as hash (a very good example is Dictionary class). This article will be split into two parts: the first one will focus on the reference types and the second one on the value types. We will focus on the internal implementation of CLR and try to figure out how exactly the hash code for reference and value types is generated.