13. The late (run-time) optimization

       Java program is initially interpreted by an interpreter, when the virtual machine to run a discovery method or block of code is particularly frequent, these codes will be identified as "hot spots Code." At runtime, the virtual machine will put that code compiled into machine code associated with the native platform, and various levels of optimization, this task is time compiler compiler called E (Just In Time Compiler referred JIT compiler ). JIT compiler is not required part of the virtual machine.

A, HotSpot in-time compiler

1. interpreter and compiler

       HotSpot virtual machine containing both the interpreter and compiler. Both have their advantages: When a program needs a quick start-up and execution, the interpreter can first play, eliminating the need for time to compile, execute immediately. After running the program, over time, gradually play a role in the compiler, after putting more and more code is compiled to native code, you can achieve higher efficiency. When the program runs more limited memory resources in the environment, can be used to explain the implementation save memory, otherwise you can use the compiler to perform to improve efficiency.

                         

                                                                                    Interactive interpreter and compiler

       HotSpot built into the two-time compiler, respectively Cilent Compiler and Server the Compile R & lt, referred to as C1 and C2 compiler compiler. Current mainstream HotSpot virtual machine (Sun series JDK1.7 and earlier), the default use of an interpreter and a compiler which directly with the work, which uses a virtual machine, the virtual machine depends on the mode of operation, according to its own HotSpot virtual machine hardware performance version of the host machine automatically select the operating mode, the user can also use the "-client" or "-server" parameter to force the operating mode specified virtual machine.

       Interpreter and compiler with a way to use is called "mixed mode" in the virtual machine. Users can use the parameters "-Xint" Forced virtual machines running in interpreted mode, then the compiler does not fully involved in the work, all the code is executed using interpreted. Use parameters "-Xcomp" forced to run in a virtual machine "b compilation mode", then the compiler will give priority to the implementation of the program the way, but still needs an interpreter to intervene in the case of the implementation process of compiling impossible.

       In order to start to achieve the best balance between responsiveness and operational efficiency program, HotSpot virtual machine uses a hierarchical compilation of strategy, JDK1.7 the Server mode as the default compiler strategy is turned on. Stratified according to the compiler to compile the compiler to optimize the size and so that, divided into different levels:

  • Layer 0: program interpreted, the interpreter does not turn on performance monitoring functions can be triggered Tier 1 compilation.
  • First layer: also called C1 compiled, the byte code compiled to native code, a simple and reliable optimization, is added if necessary to the performance monitor logic.
  • The second layer (or more layers): also known as C2 compiled bytecode is compiled to native code, but will enable some lengthy compilation optimization, even some unreliable performance monitoring information in accordance with the radical optimization.

       After the implementation of tiered compilation, Cilent Compiler and Server Compiler will work at the same time, many code may compile several times to get a higher rate compiled by Cilent Compiler, get better quality with Server Compiler compiled, interpreted and then when no order again assume the task of collecting performance monitoring information.

2. Compile the object and triggers

"Hot" There are two types:

  1. The method is called many times.
  2. The loop is executed twice.

       For the first, the overall process the compiler would compile a target, this approach is compilation of standard VM JIT compilation mode. For another, the compiler will still compile the whole method as a target. This compilation mode occurs during execution of the method, the replacement is also referred to as the stack (On Stack Replacement, referred to compile OSR).

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 . There are two main focus detection is determined by:

  • Hot-based sampling probe : virtual machine periodically checks the stack of each thread, if found one (or some) methods often appear in the top of the stack, then this method is the "hot spot method." Advantage is simple, efficient and easily access method call relationship. The disadvantage is difficult to accurately confirm the heat of a method of easily affected by thread blocks or other external factors upset the hot spot detection.
  • Counter-based detection of hot spots : the opportunity to build a virtual counter, the number of statistical methods for the implementation of each method, if the number of executions exceeds a certain threshold it is considered a "hot spot code." The disadvantage is complicated way to achieve the advantages of statistical results is relatively more accurate and rigorous.

      HotSpot is used in the second, it is for each method are prepared two types of counters: method is called counter and the back side of the counter . In determining the parameters of the virtual machines running under the premise of these two counters have a determined threshold, when the counter exceeds a threshold overflow, triggered the JIT compiler.

  • Counter method calls: the number of statistical method used is called, it is the default threshold value 1500 in the Client mode, is 10,000 in Server mode, parameter -XX: be set CompileThreshold. When a method is called, the process first checks whether there had been JIT compiled version, if present, after the priority using the compiled native code execution; if not, then this call counter value plus 1. The method then determines method calls back side of the counter and the counter value and exceeds the threshold value of the counter method calls. If exceeded, the code will submit a request to compile the method of time compiler
  • Back side counter: count the number of times it is used in a method of code execution loop. After encountering the flow of control jumps in the bytecode instructions referred to as "back side." The purpose is to establish its trigger OSR compilation. When the interpreter encounters a back side of the instruction, will first find out whether the code fragment to be executed good version has been compiled, it will take precedence if the compiled code, otherwise the back side of the counter value plus 1, then judge method calls back side of the counter and the counter value exceeds a threshold value and back side of the counter. When exceeded, OSR will submit a compilation request, and the value of the back side of the counter number is reduced, in order to continue in the interpreter loop, waiting for the output of the compiler to compile the results.

                                    

                                                               Method call trigger counter-time compilation

                                         

                                                                       Back side counter trigger-time compilation

3. compilation process

        By default, both the compilation method invocation request, or OSR compilation request, the virtual machine before the code compiler is not yet completed, are still proceed in accordance with the interpretation, but the action is compiled at compile in the background thread . Users can parameter -XX: to prohibit -BackgroundCompilation background compiler, after the ban once they reach the JIT compiler conditions, a thread of execution will submit a request to the virtual machine has been compiled to wait until after the completion of the compilation process execution native code compiler output.

       In the process of compiling the background, cilent Compiler and Server Compile different compilation of r. For  Cilent Compiler, it is a simple and fast three-compiler, the main focus is localized optimization, and give up a lot of time-consuming global optimization methods.

  • Stage 1: a front end of the platform independent byte codes constituting the intermediate code indicates a high-level (HIR). HIR static single assignment form used to represent code values. Prior to the completion of a part of the compiler optimization based on the bytecode, as with the method, constant propagation optimization will be configured like HIR completed before the bytecode.
  • 第 2 阶段 : 一个平台相关的后端从 HIR 中产生低级中间代码表示(LIR),而在此之前会在 HIR 上完成另外一些优化,如空值检查消除,范围检查消除等。
  • 第 3 阶段:是在平台相关的后端使用线性扫描算法在 LIR 上分配寄存器,并在 LIR 上做窥孔优化,然后产生机器代码。

                                      

                                                                               Cilent Compiler 架构

          Server Compiler 是专门面向服务器的典型应用。它会执行所有的经典的优化动作,如无用代码消除、循环展开、循环表达式外提,消除公共子表达式等。还会实施一些与 Java 语言特性相关的优化技术,如范围检查消除、空值检查消除等。

二、编译优化技术

下面将介绍几种最具代表性的优化技术的运行方式:

  • 语言无关性的经典优化技术之一:公共子表达式消除
  • 语言相关性的经典优化技术之一:数组范围检查消除
  • 最重要的优化技术之一:方法内联
  • 最前沿的优化技术之一:逃逸分析

1. 公共子表达式消除

       它的含义是:若一个表达式 E 已经计算过了,并且从先前的计算到现在 E 中所有变量的值都没有发生变化,那么 E 的这次出现就成为了公共子表达式。对于这种表达式,没有必要花时间再对它进行计算,只需直接使用前面计算过的表达式结果代替 E 就可以了。若这种优化仅限于程序的基本块内,称为局部公共子表达式消除。若优化的范围涵盖多个基本块,称为全局公共子表达式消除

2. 数组范围检查消除

       若有一个数组 foo[],在 Java 中访问数组元素 foo[i] 的时候系统将会自动进行上下边界的范围检查。但是数组边界检查是不是必须在运行期间一次不漏的检查是可以 “ 商量 ”的事情。例如:数组下标是一个常量,如 foo[3],只要在编译期根据数据流分析来确定 foo.length 的值,并判断下标 “3” 没有越界,执行时就无须再判断了。

3. 方法内联

       它的作用是除了消除方法调用的成本外,更重要的意义是为其他优化手段建立良好的基础。它的行为不过是将目标方法的代码 “ 复制 ” 到发起调用的方法之中,避免发生真实的方法调用。

4.  逃逸分析

       它的基本行为就是分析对象动态作用域:当一个对象在方法中被定义后,它可能被外部方法所引用,例如作为调用参数传递到其他方法中,称为方法逃逸。甚至还有可能被外部线程访问到,譬如赋值给类变量或可以在其他线程中访问的实例变量,称为线程逃逸

发布了195 篇原创文章 · 获赞 7 · 访问量 9430

Guess you like

Origin blog.csdn.net/qq_43322057/article/details/103954771