Analysis of optimization Technology of Java compiling period and operation period

Source: Internet
Author: User

The "compile time" of the Java language is a process of uncertainty, as it may refer to the process of the front-end compiler turning a Java file into a class bytecode file, or a process in which the compiler (JIT) converts bytecode into machine code during the run of the virtual machine backend. The compile-time optimizations discussed below refer to the process by which the Javac compiler translates Java files into bytecode, while run-time optimizations refer to optimizations made by the JIT compiler. compile-time optimizationThe virtual machine design team focuses on performance optimizations in the back-end instant compiler (JIT), so that class files that are not generated by the Javac compiler can also benefit from compiler optimizations. But Javac has done many optimizations for coding processes to improve programmer coding style and improve coding efficiency. Many of the new Java syntax features are implemented by the compiler's "syntactic sugar," rather than relying on the underlying improvements of the virtual machine to support it. As a result, the optimization process of the instant compiler in Java is more important to the running of the program, while the front-end compiler's optimization process during the compilation period is more relevant to program coding. The compilation process of the Javac compiler can be broadly divided into three steps: 1. Parsing and filling the symbol table process; 2. The annotation processing process of the Insert annotation processor; 3. Semantic analysis and bytecode generation process. The following are introduced separately. parse and fill the symbol table;The resolution step contains lexical analysis and grammatical analysisTwo procedures, the first lexical analysis is to transform the source code character stream into a token collection (token), and then the parsing is based on the token sequence to construct an abstract syntax tree (a tree-like representation of the syntax structure of the program code). After you complete the lexical analysis and parsing, the next step is to Fill Symbol Table, the symbol table is a set of symbolic address and symbolic information forms, the information registered in the symbol table will be used at different stages of compilation (such as the semantic analysis of the contents of the symbol table is used for semantic inspection and generation of intermediate code, the target code generation phase when the symbol name for the address assignment, the symbol table is the basis of the address assignment) annotation processing for the plug-in annotation processor:The plug-in annotation processor can be seen as a set of compiler plugins in which any element in the abstract syntax tree can be read, modified, and added. If these plug-ins modify the syntax tree during annotation processing, the compiler willback to parsing and filling symbol table process re-processingUntil all the plug-in annotation processors have not modified the syntax tree. semantic analysis and bytecode generation process:After parsing, the compiler obtains the program code Abstract Syntax tree representation, the syntax tree can represent the structure correct source program abstraction, but cannot guarantee the source program conforms to the logic, but the semantic analysis mainly is constructs the correct source program to carry on the context nature inspection. 1. Labeling CheckThe labeling check step examines the contents of whether a variable is declared before it is used, whether the data type between the variable and the assignment matches, and so on. Another important action called constant folding is also done at this stage. 2. Data and control flow analysisData and control flow analysis is a further verification of the context logic of the program, it can check whether the program local variables are assigned before use, whether each path of the method has a return value, whether all the detected anomalies are handled correctly and so on. 3. Solving grammatical sugarsSyntactic sugar is the addition of a grammar to a computer language, which has no effect on the functionality of the language, but is more convenient for programmers to use. Generics in Java, variable-length parameters, automatic unboxing and boxing, conditional compilation, and so on, are syntactic sugars, which are reverted to simple grammatical constructs during the compilation phase (e.g. List<string> and list<integer> are actually the same class during runtime). 4. Byte code generationThis process is the last phase of the Javac compilation process, where the bytecode generation phase translates the information generated by the previous steps into bytecode writing to disk, plus a small amount of code additions and conversions. Run-time optimizationIn some commercial virtual machines, Java programs are initially interpreted by the interpreter, and when the virtual machine discovers that a method or block of code is running particularly frequently, the code is identified as a "hotspot code", in order to improve the efficiency of hot-spot code execution, at run time, the virtual machine will compile the code into a local platform relatedMachine code, and perform various levels of optimization, the compiler that accomplishes this task is called an instant compiler or JIT compiler. The instant compiler is not a necessary part of the virtual machine, but the real-time compiler performance is good or bad, the level of code optimization is to measure a commercial virtual machine is one of the most important indicators of excellence. Many mainstream virtual machines contain both an interpreter and a JIT compiler, each with the advantage of a JIT compiler: When the program needs to start and execute quickly, the interpreter can work first, eliminating the time to compile and execute immediately. When the program runs, as the event progresses, the JIT compiler becomes more efficient by compiling more and more code to compile the cost code. There are two types of hotspot code that will be compiled by the instant compiler:1. The method body that is called several times; 2. The loop body that is called multiple times. The instant compiler compiles the entire method into a machine code, which is compiled as an object. This compilation is called Stack substitution (OSR) because the compilation occurs during the execution of the method. There are two ways to determine if a piece of code is a hotspot code (hotspot probing):1. Sampling-based hotspot detection: This method periodically checks the top of each thread's stack, and if one or more of the methods are found to be present at the top of the stack, then this method is the Hotspot method. The disadvantage of this approach is that it is difficult to accurately identify the heat of a method and is susceptible to factors such as thread blocking. 2. Counter-based hotspot detection: This method establishes counters for each method or even block of code, counts the number of executions of the method, and considers it a hot spot if the number of executions exceeds a threshold. Note: By default, the execution engine does not wait for the compilation request to complete, but continues to enter the interpreter to execute the bytecode as interpreted until the submitted request is compiled by the compiler. When the compilation is completed, the method's call entry address is automatically rewritten to the new address, and the compiled version is used the next time the method is called. That is, the execution engine continues to execute in an interpreted manner until the compiler is finished, while the compilation action occurs in the compilation thread in the background. Optimization Technology:In general, the native code generated by the instant compiler is better than the bytecode generated by Javac. The instant compiler employs a range of techniques to optimize code, such as common subexpression elimination, array-scoped check-out, method inline, escape analysis, and more.

Analysis of optimization Technology of Java compiling period and operation period

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.