liveBook · Manning

Just-in-time compilation

As explained in .NET in Action, Second Edition

A just-in-time compiler translates intermediate code into machine code during program execution.

A .NET application compiles to Common Intermediate Language (CIL), which does not target a particular processor architecture. Before a processor can execute that code, the CoreCLR compiles it into machine code. Its just-in-time (JIT) compiler does this at runtime, when code is about to be used, rather than compiling the entire application at startup. section 1.5.2

Compiling only needed code avoids the startup cost of compiling everything, but JIT compilation itself takes time. The .NET JIT normally uses tiers: it first produces native code quickly, then recompiles based on how the code behaves under load. Default behavior can change between .NET versions. section 1.5.2 section 10.2

Allow for JIT work in benchmarks

A BenchmarkDotNet comparison of Newtonsoft.Json and System.Text.Json deserialization runs its test methods through pilot and warmup phases before reporting timings. Those phases also give the JIT compiler time to complete, so compilation work is less likely to distort the comparison. Run the benchmark in Release mode. section 10.2

If the target processor and operating system are known, ahead-of-time (AOT) compilation can provide an initial compilation before execution instead. AOT can also trim unused code, but trimming requires thorough testing because it may remove too much. JIT can still be used later for optimization; AOT usage is not developed here. section 1.5.2

How .NET in Action, Second Edition covers it

How deep it goes
Defines runtime compilation and describes selective and tiered behavior, but stops short of compiler implementation details and AOT instructions.
Examples
Uses a BenchmarkDotNet JSON-deserialization comparison whose pilot and warmup phases allow time for JIT compilation.
What it assumes
Introduces CIL’s architecture-neutral role before explaining runtime compilation, without requiring processor-specific compiler details.
What you can do after
Evaluate/understand it when reasoning about startup costs and interpreting BenchmarkDotNet measurements.
How it connects
Connects Common Intermediate Language, CoreCLR, machine code, ahead-of-time compilation, tiered compilation, and performance benchmarking.

Also explained in

Compare how 7 books explain just-in-time compilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.