Skip to content
Tutorials Center
My learning

Topics

Lesson 165 of 182 ยท Advanced C#

IL, JIT and Tiered Compilation

See what the C# compiler really emits: read IL with a mini disassembler, learn what lambdas, async and foreach become, and how the JIT, tiering, PGO and inlining work.

When you press Run, your C# goes through two compilers. The first, Roslyn, runs on your build machine and turns C# into IL (Intermediate Language, also called CIL or MSIL), stored in your .dll or .exe. The second, the JIT (just-in-time compiler) inside the runtime, turns the IL of each method into real machine code the first time the method is called. Knowing what each stage does explains many things that otherwise look like magic: why foreach over a List<T> is fast, what a lambda really is, what await becomes, why a benchmark's first run is slower, and what "inlining" means.

This lesson makes the invisible visible. We will write a small IL reader that runs in the editor, look at the code the compiler generates for common C# features, and then follow a method through the JIT's tiers. You do not need to write IL by hand, but being able to read a listing is a professional skill, and it is easier than it sounds.

What IL looks like

IL is a stack-based instruction set. Instead of registers, instructions push values onto an evaluation stack and pop them off. To compute a + b: push a, push b, then add pops two values and pushes the sum. A method's IL, together with its metadata (names, types, signatures), is what the compiler writes.

You can read a method's IL at run time with reflection: MethodBody.GetILAsByteArray() gives the raw bytes. Raw bytes are not friendly, so the example below includes a small disassembler. It looks up each opcode in the OpCodes table, reads the operand that follows it, and resolves metadata tokens to names. The method names to inspect come from the Input box, one per line, so you can edit them and re-run.

C# A mini IL disassembler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Read the first listing, Add:

TEXT
ldarg.0     push argument 0 (a)
ldarg.1     push argument 1 (b)
add         pop two, push their sum
ret         return the value on top of the stack

Four instructions, four bytes: the compiler wrote a + b almost literally. Now Max: ble.s IL_0006 means "branch if less than or equal": pop a and b, and if a <= b jump to offset 6 (the return b). Otherwise it falls through to ldarg.0; ret. The compiler inverted your if (a > b) into a "skip the first return" branch. This is typical: control flow in IL is just conditional and unconditional jumps (br, brtrue, blt, and so on); loops and if statements are all built from them.

Greet shows another idea: the interpolated string $"Hello, {name}!" was compiled to ldstr "Hello, ", ldarg.0, ldstr "!", then a call to String::Concat. The compiler picks the cheapest strategy for the case: for simple string pieces it does not use string.Format at all. Interpolations with non-string values or format specifiers use a stack-allocated DefaultInterpolatedStringHandler (C# 10) instead. This is the kind of detail that is only visible in the IL.

The editor builds your code in Release mode. A Debug build contains extra nop instructions and temporary locals so a debugger can set breakpoints on every line, which makes IL longer and slower. Always measure and inspect IL from Release builds.

foreach, lowered

C# has many conveniences that the compiler lowers (rewrites) to simpler constructs before generating IL. Run the editor above again with the Input box set to SumArray and SumList. This is what you see for the array version (listing from our run, comments added):

TEXT
SumArray  (locals: 4, IL bytes: 28)
  IL_0000: ldc.i4.0                 total = 0
  IL_0001: stloc.0
  IL_0002: ldarg.0                  copy of the array
  IL_0003: stloc.1
  IL_0004: ldc.i4.0                 index = 0
  IL_0005: stloc.2
  IL_0006: br.s       IL_0014       jump to the condition check
  IL_0008: ldloc.1                  loop body: v = array[index]
  IL_0009: ldloc.2
  IL_000A: ldelem.i4
  IL_000B: stloc.3
  IL_000C: ldloc.0                  total += v
  IL_000D: ldloc.3
  IL_000E: add
  IL_000F: stloc.0
  IL_0010: ldloc.2                  index++
  IL_0011: ldc.i4.1
  IL_0012: add
  IL_0013: stloc.2
  IL_0014: ldloc.2                  condition: index < array.Length
  IL_0015: ldloc.1
  IL_0016: ldlen
  IL_0017: conv.i4
  IL_0018: blt.s      IL_0008       loop back
  IL_001A: ldloc.0
  IL_001B: ret

A foreach over an array is not an enumerator at all: it is an ordinary indexed for loop with the length check at the bottom. That is why it is as fast as a hand-written loop.

The List<int> version looks different. In short: callvirt List<T>::GetEnumerator, then a loop that calls Enumerator::MoveNext and get_Current on a struct enumerator (ldloca.s loads its address so the calls do not copy it), and finally a try/finally block that disposes the enumerator (constrained. callvirt IDisposable::Dispose). Because List<T>.Enumerator is a struct, there is no heap allocation, and the JIT can inline the calls. If you iterate through the IEnumerable<int> interface instead, the enumerator gets boxed and each step is an interface call. This explains the advice in the foreach lesson to keep the concrete type when it matters.

Lambdas, closures, iterators and async

Features that look like language magic are compiler-generated classes. Reflection can list them, because they are real types in the assembly, marked [CompilerGenerated].

C# The classes the compiler writes for you

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

What each generated type is for:

  • <>c__DisplayClass0_0 is a closure class. The lambda x => x > limit uses the local limit from the enclosing method, so the compiler moves limit out of the stack frame into a field of a small class, creates one instance per call, and turns the lambda into a method on that class. This is why a capturing lambda allocates and a non-capturing one does not (non-capturing lambdas are cached in a static <>c singleton). The lambdas lesson tells you how to avoid accidental captures in hot code; a static lambda makes the compiler refuse them.
  • <Evens>d__1 is an iterator state machine. Fields: <>1__state (where to resume), <>2__current (the last yielded value), n and <i>5__2 (your parameter and local, now fields so they survive between MoveNext calls). See iterators and yield.
  • <LaterAsync>d__2 is the async state machine, with a <>t__builder (here AsyncTaskMethodBuilder<int>) and a TaskAwaiter field for the pending await. In Release builds, this type is a struct, so a call that finishes synchronously allocates nothing for the machine. See async and await.

The runner shows the name of each field because the compiler gives them names that cannot be typed in C# (they contain < and >), which prevents any conflict with your own names.

Emitting IL yourself

IL is a public format, and System.Reflection.Emit lets a program write it at run time. This is how some serializers, mocking libraries and expression-tree compilers produce fast code on the fly. It needs the JIT, so it does not work under Native AOT.

C# Writing IL by hand with DynamicMethod

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

These are exactly the four instructions our disassembler printed for Calc.Add. When you call add, the JIT compiles those four instructions to machine code, just as it does for compiler-produced IL.

What the JIT does

When a method is first called, the runtime hands its IL to the JIT. In outline the JIT:

  1. Imports the IL into an internal tree representation, tracking types on the evaluation stack.
  2. Optimizes: inlining small methods, removing bounds checks it can prove unnecessary, hoisting loop invariants, devirtualizing calls when the type is known, folding constants, removing dead code.
  3. Allocates registers and emits machine code for the CPU it is running on (it can use newer instructions such as AVX2 if the machine has them, which a pre-built native binary usually cannot assume).
  4. Stores the machine code and patches the call site, so later calls go straight to it.

For Add, on a typical 64-bit Linux machine the result is about two instructions (something like lea eax, [rdi+rsi] then ret). Nothing in the machine code mentions a stack: the stack-based IL is just an interchange format, and the JIT converts it to registers.

Tiers revisited

The Native AOT lesson introduced tiered compilation. Here is the full picture, since it explains most "why is my benchmark strange" questions:

  • Tier 0 is quick, unoptimized code. Methods with loops can start here too, thanks to on-stack replacement (OSR, .NET 7), which moves a long-running loop to optimized code part-way through.
  • A method called 30 times is queued for tier 1 after a short pause (100 ms by default, restarting while new tier-0 methods keep appearing).
  • Dynamic PGO (profile-guided optimization) is enabled by default since .NET 8. Tier 0 code is instrumented to count branches and record which concrete types appear at interface and virtual calls. Tier 1 uses this to lay out hot paths first and to add guarded devirtualization: if 95% of the calls to shape.Area() are to Circle, the JIT emits "if the object is a Circle, call Circle.Area directly (and inline it), else do the normal virtual call".
  • Code that is ReadyToRun (precompiled) starts as optimized-but-conservative code, and hot methods are still rejitted at tier 1.

Inlining

Inlining replaces a call with the body of the callee. It removes call overhead and, more importantly, unlocks further optimizations. Calling Add(x, 1) with a constant can collapse into x + 1. The JIT inlines small methods automatically (roughly: a very small IL size is always a candidate, larger methods need a good estimated benefit), and refuses for methods with try/catch, loops in some cases, or very large bodies. You can nudge it with attributes, which are visible in the method's metadata:

C# Seeing the JIT hints on methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

AggressiveInlining is a hint, not a command. NoInlining is respected and is sometimes needed deliberately: a method that only throws is kept out-of-line (the "throw helper" pattern used in the framework), so the hot method stays small enough to inline. AggressiveOptimization compiles at full optimization immediately, which skips tier 0 but also skips PGO instrumentation, so it can produce worse code for long-running steady-state loops. Use these attributes only after measuring; the JIT's default decisions are usually good.

Looking at the real thing

You will not see JIT output in our editor, but on your own machine you can:

Terminal
# Read-only: on your machine, with a Release build of your project.
# Prints the machine code the JIT generates for methods named Add (any class).
DOTNET_JitDisasm="Add" dotnet run -c Release

# Useful switches for experiments:
DOTNET_TieredCompilation=0   # only fully optimized code, no tiers
DOTNET_TieredPGO=0           # turn off dynamic PGO
DOTNET_ReadyToRun=0          # ignore precompiled framework code

For IL, use a decompiler such as ILSpy (set its language to IL), the ildasm tool, or an online viewer that shows IL, C# lowering and JIT assembly side by side. They show the same listings as our mini disassembler, with more polish.

Practical takeaways

  • Cheap-looking C# can be expensive, and the reverse. A capturing lambda allocates; a foreach over an array does not. Read the IL when in doubt.
  • First-run timings lie. Tier-0 code and one-time JIT work inflate the first measurements. Warm up, repeat, and use a benchmarking tool (measuring performance).
  • Keep hot methods small and simple. That helps inlining and register allocation. Move rare paths (exceptions, logging) to separate methods.
  • Prefer sealed classes and concrete types in hot paths. The JIT can devirtualize calls when it knows the exact type.
  • Do not fight the JIT. Reach for AggressiveInlining or AggressiveOptimization only with a benchmark that proves they help.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is IL?

    Roslyn compiles C# to IL stored in the assembly. At run time the JIT compiles IL to machine code for the current CPU.
  2. 2. How does the compiler lower foreach over an int[]?

    For arrays, the compiler generates a plain indexed loop, which is why it is as fast as writing the for loop yourself.
  3. 3. Why does a lambda that uses a local variable from its enclosing method allocate?

    Captured locals become fields of a display class, created for each call. A non-capturing lambda can be cached in a static instance.
  4. 4. What does dynamic PGO let the JIT do?

    Tier 0 code records branch counts and receiver types. Tier 1 uses them for hot/cold layout and guarded devirtualization.
  5. 5. What is true about [MethodImpl(MethodImplOptions.AggressiveInlining)]?

    It is a strong hint but not a guarantee; the JIT can still refuse, for example for methods with certain constructs.

Summary

  • Roslyn compiles C# to stack-based IL; the JIT compiles each method's IL to machine code on first use.
  • You can read IL with reflection (GetMethodBody) or tools like ILSpy; Release builds give the meaningful listings.
  • The compiler lowers high-level features: foreach over arrays becomes an indexed loop, interpolation becomes Concat or a handler, lambdas with captures become closure classes, iterators and async become state machines.
  • DynamicMethod and ILGenerator emit IL at run time; they need the JIT.
  • The JIT uses tiers, on-stack replacement and dynamic PGO, and optimizes with inlining, devirtualization and bounds-check removal.
  • Use AggressiveInlining, NoInlining and AggressiveOptimization sparingly and only with measurements.

Next: C# in Games and Desktop