C# JIT compiling and .NET

Yes, JIT’ing IL code involves translating the IL into native machine instructions.

Yes, the .NET runtime interacts with the JIT’ed native machine code, in the sense that the runtime owns the memory blocks occupied by the native machine code, the runtime calls into the native machine code, etc.

You are correct that the .NET runtime does not interpret the IL code in your assemblies.

What happens is when execution reaches a function or code block (like, an else clause of an if block) that has not yet been JIT compiled into native machine code, the JIT’r is invoked to compile that block of IL into native machine code. When that’s done, program execution enters the freshly emitted machine code to execute it’s program logic. If while executing that native machine code execution reaches a function call to a function that has not yet been compiled to machine code, the JIT’r is invoked to compile that function “just in time”. And so on.

The JIT’r doesn’t necessarily compile all the logic of a function body into machine code at once. If the function has if statements, the statement blocks of the if or else clauses may not be JIT compiled until execution actually passes through that block. Code paths that have not executed remain in IL form until they do execute.

The compiled native machine code is kept in memory so that it can be used again the next time that section of code executes. The second time you call a function it will run faster than the first time you call it because no JIT step is necessary the second time around.

In desktop .NET, the native machine code is kept in memory for the lifetime of the appdomain. In .NET CF, the native machine code may be thrown away if the application is running low on memory. It will be JIT compiled again from the original IL code the next time execution passes through that code.

Leave a Comment