JIT-Just In Time Compiler

Vinayak Kokane
5 min readMay 19, 2021

--

In this blog, we are gonna learn about the JIT compiler. You might have heard this type of compiler while studying Java or .NET framework. So before the JIT compiler, let's learn about the compiler and interpreter first.

What are Compiler and Interpreter?

Compiler

The compiler is nothing but the software that takes high-level language’s code as an input and generates low-level language at once. The low-level language is in such a form that the machine can understand(machine-level language). It acts as a translater in a nutshell. The program that we provide as an input to the compiler is a source program and the converted machine language is an object code.

Hence the compiler is a very powerful tool for high-level languages. But is a bit tedious job of error finding in the compiler as it checks the whole source code first and then gives the errors. And also if the size of the program is too large, compilers take a hell of a time. Here comes the need for an interpreter, they can reduce these demerits but it itself has some :( Let's look at an interpreter.

Comparison between Compiler and Interpreter

The interpreter is just like other software, it converts High-level language instruction, line by line, into machine language. As it is executed line by line which helps programmers to find errors easily. But takes more time to execute a program than the compiler.

Why NOT Both? Why don't we combine them?

What is JIT?

JIT- Just In Time (Also known as dynamic translation or runtime compilation) is a method to improve the runtime performance of computer programs based on byte code. Bytecode is computer object code that is processed by a program, usually referred to as a virtual machine.

It converts bytecodes into machine instructions and sends them to the processor directly. The key thing about the JIT compiler is that it runs after a program starts and compiles code. So JIT compiles code on the fly and hence it is just-in-time.

The basic idea is simple. We first identify the “hot” components( eg. Functions that have used tons of time) of source code and then transform those components into machine language instructions during run-time. Optimize that machine code and swap the previous implementation by optimized the machine code version.

There are three types of JIT compilers:

  1. Pre-JIT:- Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
  2. Econo-JIT:- Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are removed when they are not required.
  3. Normal-JIT:- Normal-JIT compiles only those methods that are called at runtime. 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.

JIT compiler in Java

Byte code in Java is the most important feature which aids cross-platform execution. Conversion of bytecode into machine code for execution makes a huge impact on the total performance of Java. These bytecode have to be interpreted which is affects the execution speed

In order to improve execution speed and overall performance, JIT compilers interact with JVM (Java Virtual Machine) during run-time. The instruction which is executing multiple times will be stored in memory so that whenever that same instruction occurs again it will get machine code from memory, and hence no need to compile that instruction again.

JIT in JVM

The JIT compiler accelerates the performance of the application by compiling the bytecode into native machine code. It is enabled by default when a method is invoked. The JVM directly invokes the compiled code of the method without interpreting it. It does not require much memory usage.

The compiler uses so many code-optimization techniques, so does JIT! In JVM, JIT incorporates optimization tricks such as data analysis, translation from stack operations to register operations, reduction of memory accesses by register allocation, elimination of common sub-expressions, Dead code eliminations, etc.

JIT compiler in .NET

JIT resides in Common Language Runtime(CLR) in .NET which executes the .NET programs. .NET’s own specific compiler converts the source program into an intermediate code and then the JIT converts intermediate code into machine language code. The intermediate code is known as Microsoft Intermediate Language(MSIL) or Common Intermediate Language(CIL). JIT converts MSIL or CIL when required rather than doing it for the whole of it.

Advantages of JIT

  1. JIT compiler consumes memory very little.
  2. It runs after the start of the program.
  3. Optimization of code is done while run-time only.
  4. Page faults can be reduced because the methods required are on the same memory page.
  5. Code that is used together will be localized on the same page.
  6. JIT compilers are better able to support runtime code evaluation since the compiler is part of the runtime itself.

Disadvantages of JIT

From the start of this blog, we have been appreciating the JIT for making life so easy but it also has some disadvantages

  1. JIT compilers take more time for a startup while the application is executed initially.
  2. It uses a lot of Cache memory as it stores the source code methods during run-time.
  3. The trade-off for JIT compilation is that while the process’s goal is to increase runtime performance, the process actually occurs at runtime as well, and so it incurs overhead while analyzing, compiling, and validating code fragments. If the implementation is inefficient or not enough optimizations occur, then it actually produces a performance degradation.
  4. JIT compilers are harder to write (not a strong point but worth mentioning).

So in this blog, we have seen A to Z of JIT compilers, its real-time examples in different programming languages, and so on.

Authors: Shreya Hirapurkar, Vinayak Kokane, Dhanraj Kore, Vaishnavi Kulkarni.

THANK YOU….!

--

--

Vinayak Kokane
Vinayak Kokane

Responses (1)