What is the JIT

JIT stands for just in Time, a just-in-time compiler.

At run time the JIT saves the translated machine code for future use, so in theory it can be close to previous pure compilation techniques. Let’s take a look at the JIT process.

Note: Register use is a very common optimization for compilers. Registers are much faster than main memory.

How to optimize JIT compilation

  • Primary tuning: client mode or server mode
  • Intermediate compiler tuning (-cient, -server or -xx:+TieredCompilation)
    • Optimize code cache (-xx :ReservedCodeCacheSize)
    • CompileThreshold (-xx :CompileThreshold)
    • Check the compilation process (XX:+PrintCompilation)
  • Advanced compiler tuning
    • Compile thread (-xx :CICompilerCount)

From an optimization perspective, the easiest option is to use the hierarchical compilation technique of the Server compiler, which will solve approximately 90% of the performance problems directly related to the compiler. Finally, make sure that the code cache size is set to a large enough size that the compiler will provide the highest compilation performance.

JIT compiler in HotSpot

1.1 Compilers and interpreters

HotSpot has both a compiler and an interpreter.

There are two JIT compilers built into HotSpot:

The JVM is automatically selected based on its version and machine hardware performance

  • Client Compiler (C1 for short) – The Client parameter is mandatory
  • Server Compiler (C2 for short) – Server parameter mandatory

Mixed Mode for interpreter and compiler

  • Use the -xint parameter to force the JVM to run in explain mode, all in explain mode, without compiler involvement
  • -xcomp forces the JVM to run in compiled mode, preferably compiled mode

Hierarchical compilation: Different compilation levels are divided according to the scale of compilation and optimization time

  • At layer 0, the program is interpreted and the interpreter does not turn on the performance monitoring function, triggering layer 1 compilation
  • Layer 1, also known as C1 compilation, compiles bytecode into native code for simple, reliable optimizations and, if necessary, performance monitoring logic
  • Layer 2 (or more), also known as C2 compilation, compares bytecode to native code, but turns on some optimizations that take longer to compile, or even some aggressive optimizations that are unreliable based on performance monitoring information

After hierarchical compilation, Client Compiler and Server Compiler will work at the same time, and the code may be compiled multiple times, with higher compilation speed with Client and better compilation quality with Server, explaining execution without collecting performance monitoring information

1.2 Compiling objects and triggering conditions

There are two types of hotspot code:

  • A method that is called multiple times
  • The body of a loop that executes multiple times will actually compile the entire method as well

There are two main ways to judge hot spots:

Sample Based Hot Spot Detection: Periodically check the top of the stack of each thread, and find that certain (certain) methods often appear at the top of the stack, that is, the hot method is a little simple and efficient, can obtain the method call relationship (call stack expansion can be) disadvantage is that it is difficult to accurately confirm the heat of the method, Counter Based Hot Spot Detection (Counter Based Hot Spot Detection) : A Counter is established for each method (even code block), counting the number of method calls. If the execution exceeds the threshold, it is considered as a Hot method. The disadvantage is that implementation is difficult. The advantage is that the results are more accurate.

Counter – based probes 1500 in Client mode and 10000 in Server mode according to -xx :CompileThreshold. Calls a method that checks for jIT-compiled version native code, favors native code if it does, and incrementing the counter if it does not. It then determines whether the sum of the call counter and the return counter is greater than the threshold, and if so, the JIT compiler submits the compilation request. The method call entry is replaced by the system after JIT compilation. The compiled version is called next time. Counter Decay exceeds a certain time limit, the number of method calls has not reached the threshold, and the method Counter is reduced by half. Executed during garbage collection, closed by -usecounterdecay to count absolute counts. Use -xx :CounterHalfLifeTime to set the half-lifetime decay period. Back Edge counter: counts the number of times the method body code is executed in a method, and the instruction that controls the flow beat in the bytecode becomes Back Edge. Back side counter threshold can be used – XX: OnStackReplacePercentage to indirect adjustment. Return counter has no heat decay process.

1.3 Compilation Process

By default, the JVM interprets just-in-time compile requests until the compile is complete, with the compile action executed in the background thread

The -xx :-BackgroundCompilation disallows BackgroundCompilation, in which compile requests wait until the local code is executed directly after compilation

  • Client Compiler: Focus on local optimization, simple and fast, and give up time-consuming and long-term optimization
  • Server Compiler: Service-oriented, high-performance, complex, and slow

This article was published by YBG