Re-ordering of the JVM

Source: Internet
Author: User
Tags volatile

Reordering is typically a means by which the compiler or runtime environment performs reordering of instructions in order to optimize the performance of the program. Reordering is divided into two categories: compile -time reordering and run -time reordering, which correspond to the compile and runtime environments, respectively.

In concurrent programs, programmers will pay special attention to the synchronization of data between different processes or threads, especially when multiple threads modify the same variable at the same time, must take reliable synchronization or other measures to ensure that the data is correctly modified, here is an important principle: do not assume the order of instruction execution, You cannot predict in what order the instructions between different threads will be executed .

But inSingle Threadprogram, it is often easy to assume that directives areSequential Execution, or you can imagine what a horrible change would happen to the program. The ideal model is that the order in which the various instructions are executed is unique and orderly, which is the order in which they are written in the code, regardless of the processor or other factors, a model calledSequential Consistency Model, it is also a model based on von Neumann system. Of course, this assumption is reasonable in itself and rarely happens in practice, but in fact,no modern multi-processor architecture uses this modelBecause it is too inefficient to be in. In the compilation optimization and the CPU pipelining, almost all involves the instruction reordering.

Compile-time reordering

The typical re-ordering of compiling period is to minimize the number of registers read and stored, and to fully reuse the stored values of registers by adjusting the order of instruction without changing the semantics of the program.

Suppose the first instruction calculates a value assigned to a variable A and holds it in the register, the second instruction is independent of a but requires a register (assuming it will occupy the register where A is located), the third instruction uses the value of a and is not related to the second instruction. If, according to the sequential conformance model, A is placed in the register after the first instruction is executed, a no longer exists when the second instruction executes, and a is re-read into the register when the third instruction executes, and the value of a is not changed in this process. Normally the compiler swaps the position of the second and third directives so that a is present in the register at the end of the first instruction, and then the value of a is read directly from the register, reducing the overhead of repeated reads.

The significance of reordering for pipelining

Modern CPUs almost all use pipelining mechanism to speed up the processing of instructions, in general, an instruction requires a number of CPU clock cycle processing, and through the pipeline parallel execution, you can execute several instructions in the same clock cycle, the practice is simply to divide the instruction into different execution cycles, such as reading, addressing, Analysis, execution and other steps, and placed in different components of the processing, while in the implementation of the EU, the functional units are divided into different components, such as addition elements, multiplication elements, loading elements, storage components, etc., can further realize the parallel execution of different computations.

The pipelining architecture determines that the directives should be executed in parallel, rather than in the sequential model. Reordering is beneficial to the full use of pipelining, and thus to the effect of exceeding the quantity .

ensure sequential

Although directives are not necessarily executed in the order in which we write them, it is no doubt that in a single-threaded environment, the final effect of instruction execution should be consistent with its effect in sequential execution , otherwise this optimization loses its meaning.

The above principles are usually met in order to reorder the instructions, whether in the compile or run time.

Reordering in the Java storage model

JVM can sort them arbitrarily to improve program performance .

volatile keyword can The visibility of the variable is guaranteed , because the volatile operations are in main memory, and main memory is shared by all threads, where the cost is sacrificing performance, not using registers or caches , because they are not global, cannot guarantee visibility, and may produce dirty reads.

volatileAnother function is to partially prevent reordering, and the manipulation instructions for volatile variables will not be reordered, because if you reorder, you may have visibility issues.

Locks (including explicit locks, object locks) and read-write to atomic variables ensure the visibility of variables in terms of guaranteed visibility. However, the implementation is slightly different, such as a synchronous lock to ensure that the lock is re-read from memory into the data refresh cache, release the lock when the data is written back to memory to ensure that the data is visible, and the volatile variable is simply read and write memory.

Re-ordering of the JVM

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.