First, compiler overviewThis article describes the hotspot virtual machine compilation process, Hotspot contains an interpreter javac and two real-time compilers, they work together.
Interpreter and Compiler:
1, the interpreter and compiler each have advantages, when the program needs to quickly start and execute, the interpreter can play a role, save the compile time, immediate implementation. After the program runs, over time, the compiler will gradually play a role, the more and more code compiled cost code, you can achieve higher execution efficiency.
2, when the memory resource limit is large, you can use the interpreter to save memory, on the contrary, you can use the compiler to perform the promotion efficiency.
3, the interpreter can be as a compiler radical optimization of an escape door, when radical optimization is not tenable can be returned to the interpreter state through inverse optimization to continue execution.
Ii. The implementation process of Javac Third, the immediate compiler implementation processThere are two just-in-time compilers built into the Java virtual machine, named client and server or C1 and C2, respectively.
The C1 compiler compiles bytecode into local code for simple and reliable optimizations, and the C2 compiler initiates some time-consuming optimizations for compilation, or even some unreliable radical optimizations based on performance monitoring information. C1 and C2 are compiled some hot code (multiple call methods or multiple execution of the loop body), so before compiling, the first to be hot detection, hotspot virtual machine is based on the counter hot spot detection methods.
It prepares two counters for each method, and the method calls the counter and the back-edge counter. The method call counter counts not the absolute number of times a method is invoked, but rather a relative execution frequency, that is, the number of times a method is invoked over a period of time. When a certain time limit is exceeded, if the method call is still not enough to submit it to the Just-in-time compiler, the call counter of this method is generally attenuated, a process called the method call counter heat decay.
The back counter counts the number of times the loop body code executes in a method, and it has no heat attenuation. The purpose of setting back counter statistics is to punish OSR compilation, OSR is the stack on the replacement, that is, the compilation occurs in the method implementation process.
1. Method call counter triggers Just-in-time compilation 2, back-edge counter trigger OSR compilation Four, the compiler process of the immediate compiler1, C1 compilation processFor C1, it is a simple and fast three-segment compiler that focuses on local optimization and discards many time-consuming global optimizations.
1, in the bytecode on a number of basic optimization, methods inline, constant propagation.
2, the bytecode is constructed into a high-level intermediate representation hir (static single allocation), and then some optimization, null check elimination, range check elimination.
3. Convert hir into Lir, register allocation, Peek Hole optimization, machine code generation
2, C2 compilation processC2 performs all the classic optimization actions, such as unwanted code deletion, cyclic expansion, cyclic expression extrapolation, elimination of common subexpression, constant propagation, basic fast reordering, and so on.
java vs. C + + compiler comparison1, the real-time compiler occupies is the user program's running time, has the very big pressure, causes the compiler to dare not introduce the large-scale optimization technology. 2. Java is a dynamic language type, meaning that a virtual machine is needed to ensure that programs do not violate language semantics or access unstructured memory. This means that virtual machines require frequent dynamic type checking. 3, although there is no virtual keyword in the Java language, the frequency of using virtual methods is much greater than C + +. This means that the run-time of the method to the recipient of polymorphic Anze frequency is much greater than C, C + +. 4. The Java language is a language that can be dynamically extended, and runtime loading of new classes can alter the inheritance relationships of program types, which makes many global optimizations difficult because the compiler cannot see the full picture of the program. 5. The memory of objects in the Java language is allocated on the heap, and only local variables in the method can be allocated on the stack, requiring garbage collection.
However, the Java compiler also has its own advantages, because the C + + compiler All optimizations are completed in the compiler, with run-time performance monitoring based on optimization measures it can not be done, such as calling the flat rate prediction, branch frequency prediction cropping.
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