Illustrate the use of .NET compilation Just-in-Time Compilation in VB.NET

We have Practice on improvements to startup time for a .NET Windows app in the past few weeks. In the next few weeks I am going to share this experience and post JIT compilation and some interesting things about it.

Let's start. Today We are going to talk about JIT compilation: What is this and how it works.

There are two types of compilers: compilers who compile and compile that do one.

A clear compilation is a process when the compiler changes the source code directly to the machine code, which can be understood by the CPU. C and C ++ compilers are great examples of clear compilation.

Implement Compilation is a two-step process, and requires a virtual machine to be able to execute your code. The first step of the process is to convert your program into a bytecode interpreted by the virtual machine. The .NET Bytecode is called the Common Intermediate Language or CIL. It is also known as Microsoft Intermediate Language (MSIL) or only Intermediate Language (IL). We are going to use its official name Common Intermediate Language (CIL). The first step is done by a compiler (C #, VB.NET, etc.).

The second step is transforming the CIL code into a metal-moving machine code. This is the work of virtual machines. The Common Language Runtime (.NET Virtual Machine) converts only the executed CIL fragments in the CPI instructions on the runtime.

Just-In-Time Compilation:
The process of converting the just-in-time compilation of CIL to machine code translation This is done by the JIT compiler (JIT or Jitter) in the world of .NET, which is a part of Common Language Runtime

Let's take a look at how it works. You run a .NET application First, CLR creates an internal data structure for all types of types and their methods. Every method refers to an instruction that compiles machine instructions to CIL. The next CLR sees that the compiler reads CIL from metadata, verifies it, allocates memory and compiles biotech. Then the reference of the JIT compiler is replaced with reference to the compiled block of code. The last step is to jump on the machine and run it.

Next time, when the program says the same method, CLR executes compiled CPU instructions.

This process adds a little overhead to the first method call. Usually your app calls frequent methods, and you will not see any performance issues.

JIT store compiled code in dynamic memory This means that if you run it twice, you will compile your application twice

The jitter does not run because it is checking the status of your current system (CPU type, CPU calculation, etc.) and tries to generate custom code for your current situation. When you run next time, you may have different system status, and the output of the jitter will also be different.

JIT types:

There are two types of JIT compilers available in the latest .NET Framework version.

General - JIT compiler. This is the compiler that I have described above. It compiles methods in runtime as needed.

Pre-JIT compiler It compiles the entire assembly before being used. Usually the compilation application occurs during deployment. You can pre-compile the assembly using the Ngen.exe tool (Original Image Generator).

The third JIT compiler in the .NET Framework 1.0 and 1.1 - was Econo-JIT. This was a runtime compiler, which was removed if machine code of any customization and methods was not removed. This was done to improve compilation time. The compiler is obsolete from .NET 2.0 and you can not enable it anymore.

JIT vs Ngen

In the end, I would like to compare Normal-JIT compiler and the pre-JIT compiler (Ngen).

Normal-JIT compiler is the best choice for almost all cases because

It can take advantage of your current hardware. For example, to get this better performance, the CPU will use specific instructions.

It will analyze your execution flow and remove unused loops and branches.

When the application works, and works, and works, Jitter definitely works well for server-side applications.

The only case when you can start thinking about Ngen is Windows customers. Tell me, you have a large Windows client application, startup can take a long time, because JIT compilation will take a lot of time and CPU. This is not a problem for server-side applications, but there may be a problem with software for real users: People do not like to wait for 5 minutes to be able to run an app.