Compilers are tools that translate source code to machine understandable language. In .Net, the CLR provides a compiler that converts that source code to Microsoft Intermediate Language (MSIL).
Just-In-Time compiler(JIT) is a part of Common Language Runtime (CLR) in .NET which is responsible for managing the execution of .NET programs regardless of any .NET programming language. A language-specific compiler converts the source code to the intermediate language. This intermediate language is then converted into the machine code by the Just-In-Time (JIT) compiler. This machine code is specific to the computer environment that the JIT compiler runs on.
One of the key aspects of the JIT is its portability. To make applications work on different platforms, the source code has to be compiled to multiple target platforms. JIT makes it easier with the two-step process. The first step (converting source code to MSIL) is platform agnostic. The second step is made easier because each target platform has a JIT in its machine, which translates the MSIL to native code.
JIT compiler has the responsibility of managing the execution of any programming language that falls under .NET framework.
Input for the JIT compilers are the created .exe or .dll (which are basically in the MSIL state). Once the user executes the exe file, JIT compilers steps in to execute its function..
Working of JIT Compiler:
The JIT compiler is required to speed up the code execution and provide support for multiple platforms. Its working is given as follows:
The JIT compiler converts the Microsoft Intermediate Language(MSIL) or Common Intermediate Language(CIL) into the machine code. This is done before the MSIL or CIL can be executed. The MSIL is converted into machine code on a requirement basis i.e. the JIT compiler compiles the MSIL or CIL as required rather than the whole of it. The compiled MSIL or CIL is stored so that it is available for subsequent calls if required.
Types of JIT Compiler
JIT compilers are of 3 types. Let’s see in detail in the next section.
1. Normal JIT
With this compiler, the required methods are compiled when called at runtime. At the same time, they are being stored in the memory cache which is known as ‘jitted’. The same method is referred from the cache for subsequent calls.
2. Econo JIT
This compiler compiles the required methods that are called at the runtime Unlike the normal JIT, the compiled code is not stored in the memory cache. Rather it is compiled as and when required. Econo JITs have lesser startup latency as they spend less time compiling. This will be useful for applications that take a longer starting time. However, this has become obsolete from the .Net 2.0 version.
3. Pre JIT
In this compiler, the entire source code, instead of the used methods, is directly converted to native code at a single stroke. It is done using a Native image generator (Ngen.exe). In this way, the native code can be used from the cache rather than invoking the JIT compiler.
These methods are compiled the first time they are called, and then they are stored in the cache. When the same methods are called again, the compiled code from the cache is used for execution.
Advantages of JIT Compiler:
The JIT compiler requires less memory usage as only the methods that are required at run-time are compiled into machine code by the JIT Compiler.
Page faults are reduced by using the JIT compiler as the methods required together are most probably in the same memory page.
Code optimization based on statistical analysis can be performed by the JIT compiler while the code is running.
Disadvantages of JIT compiler:
The JIT compiler requires more startup time while the application is executed initially.
The cache memory is heavily used by the JIT compiler to store the source code methods that are required at run-time.
The Tech Platform