Search code examples
.netclrjitngen

Why JIT if we have NGEN


Why do CLR need JIT compiler to complie IL to machine code when it has NGEN that does that work at the time of installation.

One more question, how JIT will complie IL code if it is already NGENed?


Solution

    • because many apps aren't "installed" in that sense, and don't make use of the structures that NGEN require; they are just files on a storage device (or purely in memory, if loaded from an external source)
    • because NGEN is tied to hardware, and hardware can change during a machine's life
    • because the JIT itself can be updated separately to your applications, fixing bugs and improving performance
    • because apps use things like plugins and extension points; everything might not be available when you NGEN
    • because many apps use meta-programming to emit optimized code for themselves at runtime based on the exact conditions (which could be external data not knowable until it is actually invoked at runtime - how ORMs map database columns to objects, for example)
    • because in many cases it isn't worth the overhead of NGEN - it won't improve things

    When NGEN or AOT is a good fit: great, go do that. But it isn't a magic wand that solves every problem.

    One more question, how JIT will complie IL code if it is already NGENed?

    Assuming that all the preconditions for the NGEN image being a perfect match are satisfied, then the JIT won't need to be involved.