Graal – The new JIT on the block

In order for your java code to actually run, you first have to compile your code. By running the javac command you can easily do this. The sourcecode is transformed to JVM bytecode. Now this bytecode cannot run on your machine. It needs to be changed to bytecode that your computer can understand. This is done by the interpreter of the JVM. If you would do this just once and with a basic interpreter, you would have really slow code. So the JVM has a complex compiler that translates your inefficient bytecode into highly dynamically optimized bytecode, right before it is executed. This is the Just In Time (JIT) Compiler, in a Java Hotspot VM this process is done by the C2 compiler (link).

The current compiler is very efficient and has been improved over the years it has existed. However we haven’t had any major improvements in the last year. This is because the compiler has become very complex and few people fully understand the workings. And even the people who have written it, will tell you it is nearly impossible to create big changes. The compiler is written in a dialect of C++: an unsafe language that requires a lot of extra coding in order to make it run “safe”.

Enter the new JIT compiler, Graal. If you go to the git page of Graal you will notice that is has been written in Java. This means that you get all the modern benefits of Java when developing the Compiler. IDEs, debugging, profiling, debugging, and of course a really large development community that can help you improve the compiler. By making this decision they have mitigated one of the biggest drawbacks of the old compiler.

In essence the compiler code has a relative easy interface. It converts byte[] to another byte[]. During this conversion it optimizes the code that is in the byte[] and thus improves the performance of the execution of the code. If you dive deeper in the compiler, you will notice that it becomes quite complex quite fast. This is because the compiler changes the code on the fly based on previous executions. In order to make the optimization more clear, the Graal team has created a visualizer that can help you understand the optimization that is being done on a piece of code.

Because of the way Graal is setup and optimizes the code, it allows you to also specify your own optimizations. This means that if you have a certain edge case just for your application, you can introduce your own optimization.

While the old compiler was probably tuned to its max capacity, the Graal compiler is just getting started. I believe that with the introduction of this compiler, we will see new performance increase especially in the parts of java that have been introduced in the later versions, for example with the streams API.

As for stability of the JIT compiler, I have been running my java code locally on the Graal compiler for more then 3 months now without any problems. I know that this doesn’t prove a whole lot however it for me personally it proves that Graal is a potential candidate to become the new VM of choice.

If you want to read more about the Compiler I can highly recommend the following post by Chris Seaton