Course contentsShow
← C#
Lesson 1775 of 1,90816. Performance and DiagnosticsFree lesson

Improving Startup Performance with ReadyToRun

Students will configure AOT-lite compilation to reduce application launch latency.

In this lesson, you will learn how to reduce application startup latency by leveraging ReadyToRun (R2R) compilation to shift the JIT burden from runtime to build time.

The Problem: JIT Overhead at Startup

As we’ve discussed in previous lessons regarding JIT latency, every time your application launches, the Just-In-Time compiler must translate Intermediate Language (IL) into native machine code. In large-scale services or CLI tools, this "JIT tax" creates a visible delay before the application is ready to process its first request.

What is ReadyToRun?

ReadyToRun (R2R) is an "AOT-lite" (Ahead-of-Time) compilation format. Instead of waiting for the app to start, you compile your assemblies into native code during your build process. When the runtime loads your application, it maps this pre-compiled code directly into memory, significantly reducing the amount of work the JIT needs to perform.

Unlike full AOT (which can limit reflection and dynamic code execution), R2R acts as a performance cache. If the runtime determines it can optimize a method further based on the specific CPU architecture, it can still fall back to JIT-ing that specific method, keeping your application flexible while providing the speed boost of native binaries.

Configuring R2R in your Project

To enable ReadyToRun, you simply need to modify your .csproj file. This tells the MSBuild process to generate a native image for your assembly during the publish step:

<PropertyGroup>
  <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

When you run dotnet publish -c Release, the compiler produces an R2R-enabled binary. You can verify this by checking the size of your output DLLs—R2R files are generally larger than standard assemblies because they contain both the IL and the pre-compiled native code.

Balancing Trade-offs

While R2R drastically improves time-to-first-instruction, it does come with a cost: increased disk space and binary size. Because you are shipping native machine instructions for your target platform, you must ensure you are publishing for the specific Runtime Identifier (RID) of your deployment environment (e.g., linux-x64 or win-x64).

Key Takeaway: ReadyToRun reduces startup latency by pre-compiling IL into native code at build time, allowing the runtime to skip expensive JIT initialization without sacrificing the flexibility of the .NET ecosystem.