Different Types of JIT Compilers in .NET
Mostly, programmers develop their applications using high-level programming languages that are human-readable. Using that, developers create the primary logic that the app has to perform.
Since the code is in human-understandable language, developers have to be skilled in understanding how programming is done and be clear with the logic part.
Typically, developers write the source code & send it to the machine for execution. Now, as this code is written in a programming language, it’s not in the machine-understandable form. Hence, it further needs to be compiled into different languages that machines can understand.
This is where compilers are used. In this article, we will have a look at popular JIT (Just-in-time) compilers used in the .NET framework. Your partner .NET development company can choose either JIT or AOT (Ahead-of-time) compilation. So, let’s learn more about JIT compilers.
JIT- Just-in-Time Compiler
Compilers are the platforms used for translating the source code into machine-understandable language. In the .NET framework, the Common language runtime provides one compiler that converts the source code into MSIL- Microsoft Intermediate Language.
However, MSIL cannot be used in the machine. It then needs to be translated into machine-understandable code at runtime. This is to be done by the JIT compiler of the .NET that converts it into the CPU-specific code.
It is a two-step procedure of translating the source code -> MSIL, and MSIL -> native code. It is called implicit compilation.
One of the primary features of JIT is its portability. It helps the apps work on multiple platforms when source code needs compilation on multiple target frameworks.
JIT smoothens the process of implicit compilation. The first step is platform-dependent as it converts the source code into MSIL. However, the second step is easier as each platform has one JIT machine in it that translates the MSIL into the native code.
JIT has to manage the execution of different programming languages that fall inside the .NET technology. The input files for JIT compilers have .dll or .exe which are the MSIL files. Once these files are executed, the JIT compiler will start its execution.
Generally, there are three different types of JIT compilers. We will discuss each of them in detail.
Benefits of using JIT compilers
The majority of .NET development services use the JIT compilers. Compilers may differ based on the time at which the compilation act takes place.
Just like the JIT, there are other popular techniques used for compilation like AOT Ahead of Time. Just now we will focus on JIT, which runs during the runtime, the code is compiled.
When AOT is used, code is compiled before the code runs. Hence, it’s part of the deployment process itself. Explicit compilation also uses AOT for translating the source code into a native code before the program gets executed.
AOTs are made in a way that almost all CPUs can recognize the translated code. Also, this compiler is handy in large-scale apps and consumes a lot of memory and time while compiling.
When the matter of comparison is speed, JIT is speedier than AOT, if the proper amount of memory & computer power is provided. As JIT compiles the code on the same machine where the code is being executed.
Additionally, JIT also has information about the system in which the code is executed. So, it is possible to optimize and compile only the methods that are required; and ignore the not required ones.
Here are some of the pros of using a JIT compiler:
- It uses less amount of memory as compared to other ways of compilation. It is because JIT compiles only the methods that are needed at the runtime.
- Assists in executing the code optimization based on the analysis while the code runs.
However, there are a few drawbacks too:
- When JIT is used, the initial startup time increases
- Mass usage of memory cache while being executed
Different types of JIT compilers
Here are the three different types of JIT compilers that are used in the modern software development market.
The Pre-JIT compiler
With this type of JIT compiler, it compiles the whole source code into the native code in one compilation cycle. It is done using Native Image Generator (NGen)- a precompiling procedure of CIL -> native image.
Pre-JIT compiles the .NET code from getting platform-independent to an intermediate state, and a platform-particular stage. It means that it converts the .NET app to run smoothly on both Mac, Windows, and Linux 32-bit & 64-bit to one traditional exe file that can run on any one of them.
The benefit of using a Pre-JIT compiler is that you do not have the starting compilation delay that the compiler introduces when one assembly or type is loaded for the 1st time in the code. It also enhances the warm startup time of the program.
You might be thinking, what is a warm start?
A warm start is the one where the assembly data is present in the filesystem cache so no time is wasted by the disk drive(DD) to locate it on the disk.
In contrast to this is a cold start, one you’ll get when the assembly was loaded a long ago or has never been loaded. The DD has found the file first. This process is slow. Developers care about the time of the cold start because that is the only time that is so noticeable to the user.
Normal JIT Compiler
Normal-JIT is a compiler compiling the methods called at the runtime. After the execution of this method, it is stored in the memory & is commonly known as a “jitted” method. No future compilation is necessary for this method. Other method calls are directly accessible from the cache memory.
Econo JIT Compiler
Econo JIT is a JIT compiler that compiles the methods that are called during runtime execution. However, these methods are erased when not required. Econo JIT’s idea is to spend minimum time on compiling so that startup delay is lesser for interactive apps. This princess is what you notice in the app that takes a few seconds to start.
Many cons of the Just-in-time compiler can be handled using the AOT (Ahead-Of-Time) compilation. It involves the compiling of MSIL -> machine code, so that the compilation is not required at runtime and machine code can be executed easily using native methods.
Different Types of JIT Compilers- Final Verdict
Mainly the three types of JIT compilers that we discussed in this article are used majorly in the web development trends. A Just-in-time compiler resolves the problems of giving the native code that too with a platform-independent solution.
This compiler is capable of collecting the data and finally optimizing the code as and when required. One can try using the JIT compiler while developing their future .NET apps, and ultimately take the experience of its benefits that we saw in this post.
We have a lot more to share on recent technical trends. If you are a techie, you can give us a try. Bookmark our page and visit us regularly for more updates on modern technology.
If you have any queries regarding JIT compilers, mention it below this article. Our team will contact you with the feasible solutions in no time. Till then, happy reading!
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.