57 What Is Instruction Reordering and Why Is Reordering Necessary

57 What Is Instruction Reordering and Why Is Reordering Necessary #

In this lesson, we mainly introduce what is reordering and why reordering is necessary.

What is reordering #

Suppose we write a Java program that contains a series of statements. We would expect the actual execution order of these statements to be consistent with the order in which we wrote the code. However, in reality, the compiler, JVM, or CPU may rearrange the order of the actual instructions for optimization purposes. This is called reordering.

Benefits of reordering: Improved processing speed #

You may be confused about why reordering is necessary. What are the benefits of doing so?

Let’s take a specific example.

img

On the left side of the figure are 3 lines of Java code, and on the right side are the instructions that these 3 lines of code may be converted to. It can be seen that the instruction corresponding to “a = 100” is Load a, Set to 100, Store a, which means reading the value of a from the main memory, then setting the value to 100, and storing it back. Likewise, the instruction corresponding to “b = 5” is Load b, Set to 5, Store b, and the instruction corresponding to “a = a + 10” is Load a, Set to 110, Store a. If you observe carefully, you will notice that there are two “Load a” and two “Store a”, indicating that there is some room for optimization through reordering.

After reordering, the situation is shown in the following figure:

img

After reordering, the two operations of a are grouped together, and the instruction execution becomes Load a, Set to 100, Set to 110, Store a. The instructions related to b below remain unchanged, still corresponding to Load b, Set to 5, Store b.

It can be seen that after reordering, the instructions related to a have changed, saving one Load a and one Store a. Reordering reduces the number of executed instructions, thereby improving the overall running speed. This is the optimization and benefits brought about by reordering.

3 cases of reordering #

Now let’s take a look at the 3 cases of reordering.

(1) Compiler optimization

For the purpose of optimization, compilers (including JVM, JIT compilers, etc.) may rearrange a certain degree during the compilation process. For example, if there is currently data a, it is more efficient to put the operations on a together, avoiding the time cost of reading a first, then returning to re-read a. However, reordering does not mean arbitrary sorting. It needs to ensure that after reordering, the semantics within a single thread are not changed. Otherwise, if arbitrary sorting is allowed, the program would have been logically confused long ago.

(2) CPU reordering

CPUs also have optimization behaviors. The optimization here is similar to compiler optimization, both using out-of-order execution technology to improve overall execution efficiency. Therefore, even if there is no reordering by the compiler before, the CPU may still perform reordering. We must consider the consequences brought about by reordering during development.

(3) “Reordering” of memory

There is no true reordering within the memory system, but the memory can have effects that appear similar to reordering. Therefore, the term “reordering” is put in quotation marks here. Due to the existence of caches in the memory, which is manifested as main memory and local memory in JMM, the contents of the main memory and local memory may be inconsistent, which can cause the program to behave in an unordered manner.

For example, if thread 1 modifies the value of a, but didn’t write the new result back to the main memory, or thread 2 did not read the latest value in time, then thread 2 won’t see the modification made by thread 1 to a, so the value of a seen by thread 2 is still the initial value. However, thread 2 may still see the execution effect of the code after thread 1 modifies a, which appears as if reordering has occurred.

Summary #

The above is the content of this lesson. In this lesson, we first used an example to introduce what reordering is, analyzed the benefits that reordering can bring, and introduced the three cases in which reordering may occur.