Adding Tiered Compilation in .Net Core 2.1
Tags: netcore
Published: 2021-03-03 20:51:26

---
title: Adding Tiered Compilation in .Net Core 2.1
published: true
date: 2018-09-03 09:03:56 UTC
tags: NetCore
canonical_url: 
---

One of the many great features of .Net Core is its performance, which it certainly excels at.

A few weeks back I seen a new performance feature, well preview feature in .Net Core 2.1 – called “Tiered compilation”.

**What is it**

Well you could say at its core in enables applications to not only start faster but also run faster. It has a lot to do with the JIT( **J** ust **I** n **T** ime) compilation process – first let me explain a little about JIT.

**JIT**

Leaving aside AOT compilation for now, The .Net CLR typically uses the JIT compiler to compile the MSIL against the CLR using CIL metadata to machine native code for the target machines architecture.

It performs this process either on startup with full assembly compilation when its loaded or on demand at runtime when a particular method is called – what happens here is basically that when a method is called first time it gets JIT compiled then on subsequent calls the calling code effectively just references the native/Jitted version of the method in memory.

Typically in JIT each method would be JIT compiled once either using aggressive optimization on startup sacrificing startup time but benefitting from steady state runtime performance, or simple optimization at the start sacrificing steady state performance throughout runtime.  There was typically only a single choice.

**Back to tiered compilation**

With tiered compilation you can JIT compile the same method more than once to  produce a more optimized version depending on its usage, it is hot swapped at runtime automatically for you making the decision for you.

On startup it performs a basic compilation(tier 0) but if the method gets used frequently the method is recompiled on  the background thread to produce a more optimized version(tier 1) for you to improve the runtime performance. This is sometime termed called adaptive optimization.

This in effect produces both fast startup and fast steady state performance.

Another benefit of tiered compilations is faster steady state with applications that use precompiled images shipped with the framework code of .net core, with tiered compilation it can produce more  optimized versions of the code at runtime.

**How**

In order to try it out for yourself, there are a few options

1. Assumes you have access to or are building the source code, then it is a trivial change to your csproj file by simply adding the MSBuild property :

{% gist https://gist.github.com/andrewcahill/9eda3c51cb21050dc5956bbc1abe6e4a.js %}

like below

{% gist https://gist.github.com/andrewcahill/cb14498e626976af9ab20600206312b0.js %}

2. If the app was previously built and you have no access to the source code but you have access to the appsettings file simply add

{% gist https://gist.github.com/andrewcahill/f8f6afd67fc1a925f82514906e4af69c.js %}

to the configProperties.

For example:

{% gist https://gist.github.com/andrewcahill/e805da72dde680276a630528c20b27e7.js %}

**Resources:**

- You can check out official Microsoft article here: [Tiered Compilation](https://blogs.msdn.microsoft.com/dotnet/2018/08/02/tiered-compilation-preview-in-net-core-2-1/)
- Check out the GitHub thread indicating performance comparisons of Tiered vs non-tiered apps : [GitHub](https://github.com/dotnet/coreclr/issues/18361#issuecomment-396402549)

Happy coding!

The post [Adding Tiered Compilation in .Net Core 2.1](https://aviddeveloper.com/tiered-compilation-in-net-core-2-1/) appeared first on [Avid Developer](https://aviddeveloper.com).