For a .NET 10 command-line tool, what does ReadyToRun change at startup compared with Native AOT, and what does each give up?
answer
- precompiled but not frozen
- IL still ships beside native code
- no JIT at all
- closed world, trimmed
- one binary per platform
basics
~20 sReadyToRun ships precompiled code beside the IL, so less is jitted at startup, but the JIT stays and re-jits hot methods. Native AOT has no JIT: faster startup, less memory, but trimming, per-platform binaries and no run-time code generation or assembly loading.
solid answer
~50 s**ReadyToRun** (`PublishReadyToRun`) compiles the app's assemblies to native code at publish but keeps the IL, so the files grow, typically two to three times. At startup the runtime uses the precompiled code instead of jitting most methods; the JIT is still present for what could not be precompiled, and tiered compilation replaces hot ReadyToRun methods with tier-1 code. Everything dynamic keeps working. **Native AOT** (`PublishAot`) compiles the whole app, trimmed, into one native executable with no JIT: startup and memory improve the most, but there is no `Assembly.LoadFile`, no `Reflection.Emit`, `System.Linq.Expressions` runs interpreted, every library must be trim-safe, and you build one binary per platform with no cross-OS compilation. For a small tool ReadyToRun may gain little, because the framework libraries are already ReadyToRun-compiled; Native AOT is the bigger lever if the dependencies are AOT-clean.
code
csharp · 14 linesusing System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
Console.WriteLine($"IsDynamicCodeSupported: {RuntimeFeature.IsDynamicCodeSupported}");
if (args.Length > 0)
{
Assembly plugin = Assembly.LoadFrom(args[0]);
var dm = new DynamicMethod("Answer", typeof(int), Type.EmptyTypes);
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldc_I4, 42);
il.Emit(OpCodes.Ret);
Console.WriteLine(dm.Invoke(null, null));
}go deeper
Say that ReadyToRun precompiles but keeps the JIT, while Native AOT produces a native executable with no JIT at all.
Explain why ReadyToRun files are larger, why hot ReadyToRun methods are re-jitted at tier 1, and list Native AOT's limits: trimming, no dynamic loading or Reflection.Emit, per-platform binaries.
Use the build's IL2026 and IL3050 warnings as the migration map, and know when ReadyToRun gains little because the framework is already precompiled.
Weigh startup and memory against dynamic features, per-platform build cost and diagnostics, and pick per workload rather than one mode for every app.
## Three ways the same IL reaches the CPU A .NET assembly holds IL, which must become machine code before it runs. The question is when that happens. | | Default JIT | ReadyToRun | Native AOT | |---|---|---|---| | What ships | IL + metadata | IL + metadata + native code | One native executable | | JIT at run time | Every method on first call | Only what was not precompiled, plus tier-1 rejits | None | | Typical startup | Slowest | Faster | Fastest | | Dynamic code and loading | Yes | Yes | No | | File size | Smallest | About 2-3x the IL | Includes the runtime parts it needs | | Platform | Any runtime | One runtime identifier | One runtime identifier | ## ReadyToRun: precompiled, not frozen ReadyToRun (R2R) is ahead-of-time compilation layered on the normal runtime. Setting `PublishReadyToRun` makes the publish step compile each assembly the app ships to native code for a specific runtime identifier, such as `linux-x64`. - **The IL stays.** R2R binaries contain both IL and native code, which is why assemblies typically grow to two or three times their size, and why everything that needs IL keeps working. - **The JIT stays.** Some methods cannot be precompiled, among them some generic instantiations across assemblies, some interop, hardware intrinsics the compiler cannot prove are available, and code created at run time, so the JIT handles them. - **Tiering still applies.** R2R code is less optimized than tier-1 JIT output, so tiered compilation treats it as the first tier and recompiles hot methods. - **Small apps gain little.** The .NET runtime libraries are already R2R-compiled, so an app with little code of its own sees a modest startup change. The gain grows with the amount of application code that runs at startup. ## Native AOT: a closed world Native AOT, available for console apps since .NET 7, compiles the app, its dependencies and the runtime parts it needs into one native executable. The app does not use a JIT when it runs. - **Startup and memory**: no JIT work and a smaller working set, which is why the docs single out workloads with many deployed instances. - **Trimming is mandatory**: the compiler keeps only code it can prove is reachable, so reflection over members it cannot see statically may fail. - **No dynamic loading or code generation**: no `Assembly.LoadFile`, no `System.Reflection.Emit`; `System.Linq.Expressions` always uses its slower interpreted form. - **Generics are pre-generated**: every value-type instantiation gets its own native code, which can grow the binary. - **Platform-specific**: no cross-OS compilation, and a binary built on one Linux version runs on the same or newer versions only. - **The rest of the runtime is still there**: garbage collection, exceptions and threading work as usual, and reflection over statically known types still works. ## How the build tells you what will break Setting `PublishAot` in the project also turns on the trim and AOT analyzers during normal builds, so incompatibilities surface as warnings long before you publish: 1. **IL2026** marks a call to an API annotated with `RequiresUnreferencedCode`, such as `Assembly.LoadFrom`. 2. **IL3050** marks a call to an API annotated with `RequiresDynamicCode`, such as `new DynamicMethod(...)`. 3. Each warning is a place that may fail in the published binary, even though the same code runs fine under the JIT during development. `RuntimeFeature.IsDynamicCodeSupported` lets code pick a fallback path, and the `DynamicCodeSupport` property set to `false` exercises that path without a native build. ## Choosing for a command-line tool A tool that runs for a second and exits spends a large share of its life starting up, so it is where the difference shows most. Answer three questions first: 1. Does anything load assemblies, emit IL or depend on reflection the analyzers flag? 2. Can you build and test one binary per target platform? 3. Is startup actually the complaint, measured on the real machines? Then prefer: - **Native AOT** when the dependencies are trim- and AOT-clean, the tool needs no plugins or runtime code generation, and you can build per platform; - **ReadyToRun** when something dynamic is required but startup still matters; - **plain JIT** when startup is not the problem.
- What does Composite ReadyToRun add over normal ReadyToRun?Composite ReadyToRun, available since .NET 6 through `PublishReadyToRunComposite`, compiles a set of assemblies together, so the compiler optimizes across them and fewer methods are left for the JIT. The trade-off is much slower compilation and larger output, and the assemblies must ship together. The docs recommend it mainly for apps that disable tiered compilation or self-contained Linux apps chasing the best startup.
- How can you test a Native AOT fallback path without producing a native build?Set the `DynamicCodeSupport` MSBuild property to `false`; the SDK writes the matching runtimeconfig.json switch, and `RuntimeFeature.IsDynamicCodeSupported` returns `false` under the JIT, so the code takes its fallback branch. On SDK 10.0.401, a file-based app, which builds with `PublishAot` on, already gets that switch written as `false` for a normal run.
- Does Native AOT make reflection unusable?No. Reflection over types and members the compiler can see statically, such as `typeof(Order).GetProperties()`, keeps working. What fails is reflection over members trimming removed, and anything that loads assemblies or generates code at run time. The trim and AOT warnings point at those calls during the build.
- ReadyToRun turns the app into native code with no JIT left.
- Native AOT apps have no garbage collector.
- ReadyToRun code runs faster than tier-1 JIT code at steady state.
- A Native AOT binary runs on any operating system, like IL does.
- Reflection cannot be used at all under Native AOT.
- ReadyToRun makes the published assemblies smaller.