The majority of today’s programming languages are written in human-readable form known as source code. Computers, however, cannot understand source code, and, as such, to execute or run source code, compilers are used to convert code into machine language (also known as native code) for the computer to understand the set of instructions (what code is) the programmer is issuing. This process is known as compilation.
In this programming tutorial, we will look at the different types of compilation offered in .NET, C#, and ASP.NET.
What are the Types of Compilation in .NET?
The process of compilation in .NET is performed in two ways: implicitly and explicitly:
- Explicit compilation: An explicit compiler compiles the source code into machine code prior to the execution of the program. Ahead of Time (AOT) compilers are used to perform explicit compilation so that each line of the code is understood by the CPU before the execution of the program.
- Implicit compilation: Implicit compilation takes place in two steps. First, the source code is converted into intermediate code (such as MSIL or Microsoft Intermediate Language Code) by the language-specific compiler. In the second step, this intermediate language code is converted into machine code. The difference from an explicit compiler is that, at runtime, only the executed fragment of intermediate language code is compiled into machine code. This type of compilation in .NET is called Just-in-Time (JIT) compilation.
What is JIT Compilation?
JIT is a part of the CLR (Common Language Runtime) in .NET, which is used to execute a .NET program, regardless of which .NET-supported programming language is used.
The program execution in .NET is performed using the following steps:
- A language-specific compiler converts the programming language code into the intermediate language
- The intermediate language code gets converted into machine code by the JIT compiler.
- The machine code generated is specific to the computer platform.
You can learn more about the CLR on Microsoft Learn.
How Does the JIT Compiler Work in .NET?
The JIT compiler converts the intermediate language (IL) or MSIL code into machine code. The MSIL code is then converted as per the requirements of the program only and not the whole of it. .NET also keeps the MSIL code stored so it can access the source code methods on subsequent calls.
Types of JIT Compilers in .NET
.NET has three types of JIT compilers. They are:
- Pre-JIT compiler
- Normal JIT compiler
- Econo JIT compiler
We discuss each type of .NET JIT compiler in the following section.
What is a Pre-JIT Compiler
The Pre-JIT compiler compiles the source code into machine code in a single compilation cycle. Usually, this type of compilation is performed at the time of application deployment. This is implemented in Ngen.exe (Native Image Generator). The following figure shows the typical function of the Pre-JIT compiler:
What is a Normal JIT Compiler
In normal JIT compilation, source code is converted into machine code only the first time it is called by the runtime. After that, it is stored in cache and can be accessed whenever required.
What is an Econo JIT Compiler
An Econo JIT compiler compilation method only compiles the functions which are needed at the runtime and removes them once they are no longer required. The Econo JIT compiler became obsolete with the advent of .NET 2.0, but we include it here for awareness and historical purposes.
What are the Advantages and Disadvantages of JIT Compilation in .NET
JIT compilation has the following advantages for .NET developers:
- JIT performs code optimization by performing statistical analysis.
- JIT compilation consumes less memory, as only the functions which are required at the runtime are compiled into machine code.
- JIT compilation is less prone to errors because all the functions required at runtime are on the same memory page.
Along with these advantages, JIT compilation also has its disadvantages in .NET, which include:
- The JIT compiler takes more startup time when the program is executed for the first time.
- Cache memory, which is an expensive resource, is used heavily in JIT compilation to store the source code methods, since these methods are required at runtime.