JIT Compiler in .NET
Most of the programmers create their software/application using high-level programming languages which are human-understandable. Using that, developers build the basic logic that the application has to perform or the result that needs to be achieved using it.
Since they are in a human-understandable format, developers need not go through any special training to write the logic, apart from the basic understanding of how programming has to be done.
Typically, programmers create the source code and send it to the machine to execute it. Now, since the code is written in a high-level programming language, you have to make the machine understand it. And for that, it has to be translated to a different language.
This is where compilers come into picture.
In this post, we will look into one of the popular compilers of the .NET framework called JIT.
Table of contents
- What is JIT?
- Types of JIT
- Benefits of JIT compilers
What is JIT?
Firstly, let us understand what a compiler is.
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).
However, the MSIL code cannot be executed by the machine. It further needs to be translated to the machine-understandable native code at the run time. This is done by the Just-In-Time (JIT compiler) of the .Net, which converts it to the CPU-specific code.
The two-step process of translating the source code to MSIL, and then MSIL to native code is called Implicit compilation.
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..
JIT compilers are of 3 types. Let's see in detail in the next section.
Types of 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.
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.
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.
Benefits of JIT compilers
Compilers differ based on the time at which the act of compilation takes place. Like the JIT, you also have another popular compilation technique called Ahead-Of-Time (AOT). As far as JIT is concerned, only during the runtime, the code gets compiled. In the case of AOT, the code is compiled even before the code is run - it is done as a part of the deployment process itself.
In other words, explicit compilation uses AOT compilers to translate the source code directly to native code even before the program is being executed. And AOTs are designed in such a way that any CPU can understand the translated code for execution. Also, they are handy in large-scale applications which consume a lot of time and memory for compiling.
When it comes to speed, JIT compilers have an upper hand over AOT, provided if the required memory and computational power are available. As JIT compiles the code on the very same machine where the code is getting executed, it has the required information for code optimization. Also, JIT has the information about the machine at which the code gets executed. So it is possible to optimize the code and compile only the required methods and leave out other untouched codes.
As JIT compiles the code on the machine in which it gets executed, it can compile and fine-tune it to a granular level so that it matches with the particular system from which it is getting executed.
Here are some other advantages of using JIT compiler -
- It consumes less memory as it compiles only the methods that are required at the runtime.
- Helps in performing code optimization based on the analysis while it runs the code.
At the same time, there are a couple of negatives of using a JIT compiler. They are -
- The initial start-up time of the application is high
- Heavy usage of the memory cache during runtime
JIT compiler resolves the problem of delivering native code with a platform-independent solution. Also, it is capable of collecting the execution data and optimizing the code as required. Try using the JIT compiler for your next .NET application and experience its long list of benefits highlighted in this post.