.NET 7 and On-Stack Replacement (OSR)

Tiered compilation enables the Just-In-Time (JIT) compiler to compile code multiple times.

Initially, its goal is to optimize program startup for faster execution, leaving most assembly code unoptimized. It employs instrumentation to identify frequently called code sections for further optimizations. When a specific method is called frequently enough, JIT optimizes and recompiles the code, directing subsequent calls to the optimized version.
This approach balances both throughput and startup times effectively.

However, there is a challenge. The tiered compilation isn't enabled by default for methods with loops that are infrequently called but need optimization. While an environment variable (DOTNET_TC_QuickJitForLoops) can override this default behavior, there remains an issue with methods compiled only once during the application's lifecycle, such as the Main method. These methods miss out on recompilation and optimization, staying at tier-0 compilation.

OSR in .NET 7 addresses this by instrumenting loop iterations along with the method call frequencies during initial compilation. When the loop iteration count surpasses a threshold, JIT generates an optimized code version. This real-time optimization benefits all method invocations, improving overall throughput.