In concurrent programs, programmers pay special attention to data synchronization 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 execute.
But in a single-threaded program, it's often easy to assume that the instructions are sequential, or else you can imagine what terrible changes might happen to the program. The ideal model is that the order in which the various instructions are executed is unique and orderly, the order in which they are written in the code, regardless of the processor or other factors, which are called sequential conformance models and models based on the von Neumann system. Of course, this assumption is reasonable in itself and rarely happens in practice, but in fact, no modern multiprocessor architecture uses this model because it is too inefficient. And in the compilation optimization and the CPU pipeline, almost all involve the instruction reordering.
Compile-time reordering
The typical compile-time reordering is to reduce the number of registers read and save, and fully reuse the storage value of registers, by adjusting the order of instruction and without changing the semantics of the program.
Assuming that the first instruction computes a value assigned to variable A and is stored in a register, the second instruction has nothing to do with a but needs to occupy a register (assuming it will occupy that register of a), and the third instruction uses the value of a and is independent of the second instruction. If, in the order conformance model, A is placed in the register after the first instruction executes, a ceases to exist when the second instruction executes, and a third instruction executes, a is reread into the register, and the value of a is not changed in the process. Usually the compiler swaps the position of the second and third directives so that when the first instruction ends, A is present in the register, then the value of a can be read directly from the register, reducing the overhead of duplicate reads.
The importance of reordering for pipelining
Modern CPU almost all adopt pipelining mechanism to speed up the processing speed of instruction, in general, a single instruction requires several CPU clock cycles, and in parallel execution through pipelining, several instructions can be executed within the same clock cycle, simply by dividing the instructions into different execution cycles, such as reading, addressing, parsing, execution, and other steps, and placed in different components of the processing, while in the executive unit of the EU, functional units are divided into different components, such as addition elements, multiplication elements, loading components, storage components, etc., can further implement different computations in parallel execution.
The pipelined architecture determines that instructions should be executed in parallel, rather than in the order of the model. Reordering is beneficial to the full use of pipelining and thus to the effect of exceeding the standard quantity.
Ensure the order of
Although instructions do not necessarily execute in the order we write, there is no doubt that, in a single-threaded environment, the final effect of the instruction execution should be the same as the effect in sequential execution, otherwise the optimization will lose its meaning.
The above principles are usually met in order reordering, whether at compile-time or runtime.
Reordering in the Java storage model
In Java storage models (Java Memory model, JMM), reordering is a very important section, especially in concurrent programming. JMM guarantees sequential semantics through the Happens-before law, and if a thread that performs operation B is to observe the result of the thread performing operation A, then A and B must satisfy the happens-before principle, otherwise the JVM can sort them arbitrarily to improve program performance.
The volatile keyword guarantees the visibility of the variable because operations on volatile are in main memory, and main memory is shared by all threads, at the expense of performance and inability to use registers or cache because they are not global. Visibility is not guaranteed and dirty reads may occur.
Volatile also has the role of local blocking reordering, and the operation instructions for volatile variables are not reordered, because if you reorder, you may have visibility problems.
In terms of visibility, locks (including explicit locks, object locks), and read and write to atomic variables ensure the visibility of variables. However, the implementation is slightly different, for example, the synchronous lock guarantees that the lock is read from memory to the data flushing cache, release the lock data back to the memory to protect the data visible, and the volatile variable is simply read and write memory.