JIT, trimming and Native AOT
JIT, trimming and Native AOT
Deployment mode determines which runtime and code are shipped and how the application starts; it does not by itself determine application speed.
Framework-dependent publishing uses an installed compatible runtime. Self-contained publishing bundles runtime components for the target RID; it still depends on supported operating-system libraries. Single-file bundles files but does not imply Native AOT or eliminate all extraction/native dependencies. ReadyToRun precompiles some methods while retaining IL and JIT capability, with a possible size cost. Tiered compilation can replace initial code with optimized code; dynamic PGO uses runtime profiles. Trimming removes code based on reachability analysis; dynamic reflection may require annotations or redesign. Native AOT compiles to native code at publish, is self-contained and target-specific, and restricts dynamic loading/code generation. Do not suppress trimming or AOT warnings merely to make a build succeed. Use source-generated JSON metadata when appropriate and verify all library compatibility. The companion publish lab compares framework-dependent, self-contained, single-file, ReadyToRun and AOT outputs on a reviewed target; no binary has been built. Compare actual file sizes, cold startup, steady throughput and behavior independently; record any warnings and inspect target prerequisites.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Synthetic calculations are not performance measurements.
Separate runtime ownership from packaging
A small explicit matrix is a decision aid, not runtime detection.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;foreach (var mode in new[] { (Name:"framework-dependent", Runtime:false, Jit:true), (Name:"self-contained", Runtime:true, Jit:true), (Name:"ready-to-run self-contained", Runtime:true, Jit:true), (Name:"native-aot", Runtime:true, Jit:false) }) Console.WriteLine($"{mode.Name}: bundles-runtime={mode.Runtime}; jit-capable={mode.Jit}");Expected result (not executed):
framework-dependent: bundles-runtime=False; jit-capable=Trueself-contained: bundles-runtime=True; jit-capable=Trueready-to-run self-contained: bundles-runtime=True; jit-capable=Truenative-aot: bundles-runtime=True; jit-capable=FalseThis models common modes, not all combinations. ReadyToRun can also be framework-dependent. The runtime shipped with Native AOT is not the usual JIT runtime.
Use a statically generated JSON contract
Generate metadata for a known DTO and verify a round trip.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;using System.Text.Json;using System.Text.Json.Serialization;var original = new Shipment("A", 3);string json = JsonSerializer.Serialize(original, ShipmentJson.Default.Shipment);var restored = JsonSerializer.Deserialize(json, ShipmentJson.Default.Shipment);if (restored != original) throw new Exception("round trip failed");Console.WriteLine($"{restored!.Sku}: {restored.Units}");public sealed record Shipment(string Sku, int Units);[JsonSerializable(typeof(Shipment))]public partial class ShipmentJson : JsonSerializerContext { }Expected result (not executed):
A: 3Source generation supplies a known contract without default reflection-based discovery. Actual generator output, trimming and native publish must still be verified.
Replace runtime type lookup with explicit dispatch
An allowlisted operation table preserves known references at build time.
Inspect the archived Fixture.csproj and use only an approved .NET 10 SDK. Build and run in an isolated lab with finite time and resource limits.
using System;static int Apply(string action, int n) => action switch { "double" => checked(n*2), "increment" => checked(n+1), _ => throw new ArgumentException("unsupported operation")};Console.WriteLine(Apply("double", 4));try { Apply("unknown", 4); } catch (ArgumentException) { Console.WriteLine("rejected"); }Expected result (not executed):
8rejectedStatic dispatch avoids arbitrary type loading and is easy to review. It does not prove that the entire dependency graph is AOT-compatible.
Apply the concept
Choose deployment modes for a managed server with installed runtime and a small tool requiring no installed .NET runtime. Identify what to test before choosing AOT.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JIT, trimming and Native AOT — knowledge check
Apply the lesson concepts to the following scenarios.
Does single-file imply Native AOT?
Can ReadyToRun still use JIT?
What does self-contained mean?
How should an AOT warning be handled?
What does a generated JSON context prove?