Managed object internals, Part 1. The layout

The layout of a managed object is pretty simple: a managed object contains instance data, a pointer to a meta-data (a.k.a. method table pointer) and a bag of internal information also known as an object header. The first time I’ve read about it, I’ve got a question: why the layout of an object is so…


The ‘in’-modifier and the readonly structs in C#

C# 7.2 got two very important features for high-performance scenarios — the readonly structs and the in parameters. But to understand why this additions are so important and how they’re related to each other we should look back in history. As you probably know, the .NET ecosystem has two family of types — the value…


One user scenario to rule them all

The async series Dissecting the async methods in C#. Extending the async methods in C#. The performance characteristics of the async methods in C#. One user scenario to rule them all. Almost every non-trivial behavior of the async methods in C# can be explained based on one user scenario: migration of the existing synchronous code…


Performance implications of default struct equality in C#

If you’re familiar with C#, then you most likely heard that you should always override Equals and GetHashCode for custom structs for performance reasons. To better understand the importance and the rationale behind this advice we’re going to look at the default behavior to see why and where the performance hit comes from. Then we’ll…


Dissecting the code: yet another programmer’s blog

Hi everyone. My name is Sergey Tepliakov; I’m Senior Software Engineer at TSE(*) team at Microsoft. At my day-to-day job I’m working on a next-gen build engine with tons of very useful capabilities. But don’t worry, I’m not going to dig into this topic. At least for now:) Before joining Microsoft 2 years ago, I’ve…


Garbage collection and variable lifetime tracking

Here is a seemingly simple question for you: Is it possible that the CLR will call a finalizer for an instance when an instance method is still running? In other words, is it possible in the following case to see ‘Finalizing instance.’ before ‘Finished doing something.’? internal class GcIsWeird{    ~GcIsWeird()    {        Console.WriteLine(“Finalizing instance.”);    }     public int data = 42;    …


Managed object internals, Part 3. The layout of a managed array

Arrays are one of the basic building blocks of every applications. Even if you do not use arrays directly every day you definitely use them indirectly as part of almost any library. C# has arrays from the very beginning and back in the day that was the only “generic”-like and type safe data structure available….


Avoiding struct and readonly reference performance pitfalls with ErrorProne.NET

As you may know from my previous posts “The ‘in’-modifier and the readonly structs in C#” and “Performance traps of ref locals and ref returns in C#”, structs are trickier then you might think. Mutability aside, the behavior of readonly and non-readonly structs in “readonly” contexts is very different. Structs are meant for high-performance scenarios…


The danger of TaskCompletionSource<T> class

… when used with async/await. TaskCompletionSource<T> class is a very useful facility if you want to control the lifetime of a task manually. Here is a canonical example when TaskCompletionSourceis used for converting the event-based asynchronous code to the Task-based pattern: public static Task PerformOperation(this PictureBox pictureBox){    var tcs = new TaskCompletionSource<object>();                // Naive version that does not unsubscribe…


Managed object internals, Part 4. Fields layout

In the recent blog posts we’ve discussed invisible part of the object layout in the CLR: Managed object internals, Part 1. The Layout Managed object internals, Part 2. Object header layout and the cost of locking Managed object internals, Part 3. The layout of a managed array This time we’re going to focus on the…