Chapter XI of late (run-time) optimization "in-depth understanding of java virtual machine"

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xuchuanliang11/article/details/102748237

When a virtual machine to run a discovery method particularly frequent code blocks, the code will be recognized as a hotspot code. At runtime, the virtual machine will put that code compiled to platform-dependent machine code, and various levels of optimization, this task is called a compiler-time compiler (JIT compiler)

Interpreter (Interperter) and compiler coexistence: when the program needs to quickly launch and execution, the interpreter can first play, eliminating the need for time to compile, execute immediately. After running the program, as time goes on, more and more after the compiler code compiled to native code, can achieve higher efficiency.

HotSpot virtual machine built into the two-time compiler, respectively Client Compiler and Server Compiler, referred to as C1 and C2

HotSpot compiler will gradually enable layered, stratified according to the compiler compiler to optimize the size and time-consuming to compile divided into different levels, including:

Layer 0, the program interpreted, the interpreter does not turn on performance monitoring functions can be triggered to compile the first layer

Layer 1, also known as C1 compiled, the compiled bytecode to native code, a simple, reliable optimization logic is added if necessary to performance monitoring

Layer 2, also known as C2 compiler, byte code is compiled to native code, but will enable some compiler optimization takes a long time, and even some unreliable aggressive optimization based on performance monitoring information

Obtain higher speeds compiled by Client Compiler, use Server Compiler to get better compiler quality.

Hot code has two categories: the method is called multiple times, the loop is executed twice.

A piece of code to determine the code is not hot, is not it needs to trigger in-time compilation, this behavior is called Hot Spot Detection, divided into two types:

1. Hot-based detection sample: VM stack periodic check, if a method is often in the top of the stack, it is determined that the method is a hot method. Advantage is simple, efficient, and can easily get method invocation relationship, the disadvantage is difficult to accurately identify a method for heat.

2. Based on the hot counter probe: to establish the number of executions counter, statistical methods for each method, if the number of executions exceeds the threshold value is identified as the hot method. The disadvantage is in trouble, the advantage is accurate and precise.

HotSpot-based hotspot detection counter, prepare two types of counters for each method: method call counter and back side of the counter. Two counters have determined threshold value, exceeds the threshold value JIT compiler is triggered.

Method Invocation counter : The default is 1500 times C1, C2 is 10,000 times. By -XX: CompileThreshould settings.

The default method calls the method is called counter is not an absolute number, but a relative frequency of execution, that is, the number of times within a period of time the method is called. When more than a certain time limit, if the method is still not enough to make it the number of calls presented to the in-time compiler, then call counter this method will be reduced by half, a process called attenuation.

JIT compilation process

First stage: front end of a platform-independent bytecode configured to represent an advanced intermediate code;

Second stage: the rear end of a lower platform-generated intermediate representations from the HIR;

The third stage: the rear end of the platform using a linear scan correlation algorithm, register allocation in the LIR, and do peephole on LIR, then generates machine code.

Compiler optimizations:

The first step in inline methods, the main purpose of inline approach has two, one is the cost of the removal method calls, the second is to establish a good foundation for the other optimized for easy after taking over a greater range after linking expansion within the sequence method optimization methods in order to gain better optimization results.

The second step to eliminate redundant access

The third step is performed replication spread

Step four were dead code elimination

Inline Method: java parsing and dispatching method calls, private calls only instruction invokespecial method, and examples of constructors, methods, and the parent class invokestatic static method call instruction is parsed at compile time, in addition to the foregoing 4 external method, other method calls requires java run polymorphic recipient selection method, and there may be more than one version of the method of the recipient, the java language default example method is a virtual method.

对于一个虚方法,编译期做内联的时候根本无法确定应该使用哪个方法版本,为了解决虚方法内联问题,首先 引入一种名为类型继承关系分析(CHA)的技术,这是一种基于整个应用程序的类型分析技术,它用于确定在目前以加载的类中,某个接口是否有多于一种的实现,某个类是否存在子类、子类是否为抽象类等信息。

编译器在进行内联时,如果是非虚方法,那么直接进行内联。如果遇到虚方法,则会向CHA查询此方法在当前程序下是否有多个目标版本可供选择,如果查询结果只有一个版本,也可以进行内联。如果程序在后续执行过程中,虚拟机一直没有加载到会令这个方法接收者的继承关系发生变化的新类,那么则这个内联继续使用下去,否则抛弃已经编译的代码,退回到解释状态执行,或者重新编译。

如果向CHA查询出来的结果是多个版本的目标方法可供选择,则编译器使用内联缓存来完成内联,在未发生方法调用之前,内联缓存状态为空,当第一次发生调用后,缓存记录下方法接收者的版本信息,并且每次进行方法调用时都会比较接收者版本,如果版本发生变化则会取消内联,查找虚方法进行分派,否则继续内联。

 

Guess you like

Origin blog.csdn.net/xuchuanliang11/article/details/102748237