skip to content

In .NET 10, how does tiered compilation move a method from quick JIT to optimized code, and why are a service's first requests slower?

level: middleimportance: must knowfreq 55%

answer

  1. two tiers, not one compile
  2. call counting after startup quiets
  3. background recompile with profiles
  4. loops escape through OSR
  5. three spellings of each switch

basics

~20 s

Methods start on quick tier-0 code or their ReadyToRun code; hot ones, after 30 calls once startup quiets, are recompiled in the background at tier 1 using dynamic PGO data. Early requests pay for JIT and run the slower first-tier code.

solid answer

~50 s

With tiered compilation, on by default, the JIT first produces **tier-0** code: compiled fast with minimal optimization, or the method's ReadyToRun code if it has some. The runtime then counts calls, but only after a 100 ms window with no new tier-0 JIT activity, and a method that reaches 30 calls is promoted. With **dynamic PGO**, on by default since .NET 8, it first gets an instrumented version that records the types and branches it sees; then **tier 1** is compiled fully optimized on a background thread, using that profile for devirtualization, inlining and layout, and swapped in. Since .NET 7 a method with a long loop can also start at tier 0, because **on-stack replacement** moves a running loop to optimized code. First requests are slower because they pay JIT time and run tier-0 or instrumented code. The switches are `TieredCompilation`, `TieredCompilationQuickJitForLoops` and `TieredPGO` in the project, with matching runtimeconfig.json and `DOTNET_` names.

code

xml · 4 lines
xml
<PropertyGroup>
  <TieredPGO>false</TieredPGO>
  <TieredCompilationQuickJitForLoops>false</TieredCompilationQuickJitForLoops>
</PropertyGroup>

go deeper

for a junior

Know that .NET compiles a method quickly first and better later, and that this is why an app speeds up after it has been running for a while.

for a middle

Walk through tier 0, call counting after startup quiets, the 30-call threshold and background tier-1 compilation, and name dynamic PGO as the default profile source since .NET 8.

for a senior

Diagnose warm-up: explain OSR for loop-heavy methods, why AggressiveOptimization and disabling tiering lose PGO, and how an environment variable can silently override a project setting since .NET 9.

for a principal

Treat JIT switches as fleet policy: changing tiering or PGO defaults needs measured startup and steady-state evidence, and the settings belong in one place your images and projects agree on.

## Why the JIT compiles a method more than once A just-in-time compiler faces a trade-off: optimizing code well takes time, and that time is spent while the application is starting. **Tiered compilation**, on by default since .NET Core 3.0, splits the work so that startup gets cheap code and hot code gets good code. - **Tier 0**: the first version of a method. Either the JIT compiles it quickly with minimal optimization (**quick JIT**), or the runtime uses precompiled **ReadyToRun** code shipped in the assembly. - **Tier 1**: a fully optimized version, compiled later and only for methods that prove to be hot. ## How a method gets promoted The runtime source (`clrconfigvalues.h`) and the tiered compilation design notes describe the policy: 1. A method's first call compiles it at tier 0 (or binds its ReadyToRun code) and installs call counting. 2. Call counting is delayed while startup-like activity continues: the runtime waits for a **100 ms** window with no new tier-0 JIT work (`TC_CallCountingDelayMs`). 3. Once counting is active, a method called **30** times (`TC_CallCountThreshold`) is queued for promotion; with dynamic PGO an instrumented version comes first, as the next section describes. 4. A background thread compiles the optimized version, and the method's entry point is switched so new calls use it. Those numbers are runtime defaults, not contracts. Methods marked `[MethodImpl(MethodImplOptions.AggressiveOptimization)]` skip tiering and are compiled fully optimized on their first call. ## Dynamic PGO **Profile-guided optimization** uses data about how code actually behaves. Dynamic PGO, introduced in .NET 6 and **on by default since .NET 8**, collects that data in the running process. With the default strategy, only code that is already hot is instrumented: a hot tier-0 method is recompiled with counters and type probes, runs that way for a while, and then tier 1 is compiled with the profile. The profile feeds: - **guarded devirtualization**: if an interface call almost always hits one concrete type, the JIT tests for that type and inlines its method; - **inlining and block layout**: hot paths are laid out together and cold paths moved aside. Dynamic PGO only applies to methods that go through the tiers, so `AggressiveOptimization` and disabling tiered compilation both forfeit it. ## Loops and on-stack replacement A method called once that loops for minutes would never reach 30 calls. Before .NET 7, methods with loops therefore skipped tier 0 by default. Since .NET 7, **on-stack replacement (OSR)** lets such a method start at tier 0 with patchpoints in its loops; when a loop runs long enough, the runtime compiles an optimized version and transfers the running frame into it mid-execution. The runtime source sets `TC_QuickJitForLoops` to 1 where OSR is available; the compilation settings page still describes the older off default. ## Why the first requests are slower - Every method a request touches for the first time is compiled on that request's thread. - That first code is tier-0 or instrumented code, which runs slower than tier 1. - Call counting and background tier-1 compilation add work until the hot set settles. ReadyToRun reduces the JIT work, but its code is less optimized than tier 1, so hot ReadyToRun methods are still promoted. ## The switches and their three spellings | Setting | runtimeconfig.json | MSBuild property | Environment variable | |---|---|---|---| | Tiered compilation | `System.Runtime.TieredCompilation` | `TieredCompilation` | `DOTNET_TieredCompilation` | | Quick JIT | `System.Runtime.TieredCompilation.QuickJit` | `TieredCompilationQuickJit` | `DOTNET_TC_QuickJit` | | Quick JIT for loops | `System.Runtime.TieredCompilation.QuickJitForLoops` | `TieredCompilationQuickJitForLoops` | `DOTNET_TC_QuickJitForLoops` | | Dynamic PGO | `System.Runtime.TieredPGO` | `TieredPGO` | `DOTNET_TieredPGO` | The SDK writes the MSBuild properties into the app's `runtimeconfig.json`. **Since .NET 9, an environment variable takes precedence** over both the MSBuild property and runtimeconfig.json, which is a common surprise when a container image sets `DOTNET_` variables. ## Observing it - `System.Runtime.JitInfo.GetCompiledMethodCount()` and `JitInfo.GetCompilationTime()` report how many methods the JIT has compiled and how long it spent, process-wide or for the current thread; sampling them during startup shows how much JIT work the first requests pay for. - `AppContext.GetData("System.Runtime.TieredPGO")` returns the value the SDK wrote into runtimeconfig.json, which confirms what the project asked for; an environment variable can still override it at run time. - Timing numbers depend on the machine and the workload, so compare modes on the same hardware rather than trusting a single run. ## Common mistakes - Turning off tiered compilation to "skip warm-up": every first call then pays full optimization, startup slows, and dynamic PGO is lost. - Assuming a method is optimized after one call. - Treating ReadyToRun code as final.

  • Would you disable tiered compilation to get rid of warm-up?
    Usually not. With `TieredCompilation` off, every method is compiled fully optimized on its first call, so startup gets slower, and dynamic PGO is lost because it relies on tiering; ReadyToRun code is then kept rather than replaced. Measure first: ReadyToRun to cut JIT work, or warming the hot paths before taking traffic, usually address the symptom with fewer side effects.
  • When is MethodImplOptions.AggressiveOptimization justified?
    Rarely: for a method whose tier-0 version measurably hurts, such as a startup-critical computation. It makes the JIT compile the method fully optimized on its first call and skip tiering, which also means the method never gets dynamic PGO data. It is a measured exception, not a default.
  • A container image sets DOTNET_TieredPGO=0 and the project sets TieredPGO to true; what does a .NET 10 process do?
    It runs with dynamic PGO off. Since .NET 9, `DOTNET_` environment variables take precedence over both MSBuild properties and runtimeconfig.json; before .NET 9 the runtimeconfig.json value won. Check the image's environment when a runtime setting seems ignored.

  • A method is promoted to fully optimized code after its first call.
  • Turning tiered compilation off always makes a service faster.
  • Dynamic PGO has to be switched on with TieredPGO in .NET 10.
  • Methods that contain loops can never start in quick JIT code.
  • ReadyToRun code is final; the JIT never recompiles those methods.
  • The project file's TieredPGO value always beats a DOTNET_ environment variable.

Assumes .NET 10: dynamic PGO is on by default since .NET 8, OSR lets loop methods start at tier 0 since .NET 7, and DOTNET_ environment variables override runtimeconfig.json since .NET 9.