Understanding the optimizations performed by JIT
This section will cover the various optimization techniques that the JIT compilers employ at the various levels of compilation.
Inlining
Calling a method is an expensive operation for JVM. When the program calls a method, JVM has to create a new stack frame for that method, copy all the values into the stack frame, and execute the code. Once the method completes, the stack frame has to be managed post-execution. One of the best practices in object-oriented programming is to access the object members through access methods (getters and setters).
Inlining is one of the most effective optimizations performed by JVM. JVM replaces the method call with the actual content of the code.
If we run our previous code with the following command, we can see how JVM performs inlining:
java -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining Sample
In this case, the call to the performOperation()
method is replaced...