Phase 1 of 30 · Topic 1.2

RyuJIT Tiered Compilation & Dynamic PGO (Profile-Guided Optimization)

1Concept

In .NET 8/9, RyuJIT employs Tiered Compilation. Tier 0 compiles code quickly without aggressive optimizations. Dynamic PGO observes running loops and hot paths, recompiling them at Tier 1 with vectorization (AVX-512) and method devirtualization.

2Architecture Diagram

Method Invocation
       │
       ▼
 [ Tier 0: Quick JIT ] ──> Fast startup, unoptimized, instrumented with counters
       │
       │ (Call Count > Threshold)
       ▼
 [ Dynamic PGO Profiler ] ──> Records exact type distributions & branch frequencies
       │
       ▼
 [ Tier 1: Optimized JIT ] ──> Inlining, loop unrolling, SIMD vectorization

3Code Example

C# 13 & .NET 9
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

public class TieredJitDemo
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static double ComputeHeatmap(double x, double y)
    {
        return Math.Sqrt(x * x + y * y);
    }

    public static void Main()
    {
        // Warmup loop: triggers RyuJIT Tier 1 recompilation
        for (int i = 0; i < 50_000; i++)
        {
            ComputeHeatmap(i * 0.1, i * 0.2);
        }
        Console.WriteLine("Method promoted to Tier 1 Optimized Native Code.");
    }
}

4Expected Output

Method promoted to Tier 1 Optimized Native Code.

5Key Takeaways

  • Tier 0 provides instantaneous application startup without JIT stalls.
  • Dynamic PGO allows C# to achieve or exceed native C++ performance on hot loops.
  • Check JIT tiering using `DOTNET_TieredCompilation=1` and `DOTNET_DynamicPGO=1`.