Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are hoisting and reordering the same thing?

I read from Effective Java that In the absence of synchronization the following sequence A below can be converted into sequence B by the virtual machine and this is called hoisting. I also read somewhere that if variables are not declared as volatile instructions involving the variables can be reordered . Are hoisting and reordering the same thing?

  while (!done)    sequence A     
    i++;

  if (!done)
     while (true)    sequence B
        i++;
like image 544
Inquisitive Avatar asked Jul 11 '12 09:07

Inquisitive


People also ask

Does C++ have Hoisting?

Hoisting does not exist in C, C++, Java. Variable declaration can occur at any point within a method or function for C++ and Java but it must be before the value is used. For C it must be at the top. As of C99 they no longer need to be at the top (as described in the other answer).

What is hoisting in Java?

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.

Are assignments hoisted?

What is Hoisting? Hoisting is when the JavaScript interpreter moves all variable and function declarations to the top of the current scope. It's important to keep in mind that only the actual declarations are hoisted, and that assignments are left where they are.


2 Answers

They are slightly different.

Hoisting means that you have pulled some operation out of a loop because the loop itself does not affect the result of the operation. In your case, you are hoisting the conditional test out of the while loop.

Re-ordering means changing the sequence of instructions in a way that does not affect the result. Typically this would be adjacent instructions with no data dependencies, e.g. it does not matter which order you perform the following two statements:

int a = x;
int b = y;
like image 70
mikera Avatar answered Oct 20 '22 17:10

mikera


The term "reordering" as it's used in the Java Memory Model refers to all possible optimizations that may affect correctness of improperly synchronized programs.

So, reordering in this sense is a generic term that includes optimizations such as hoisting, effects of out-of-order execution, inconsistencies caused by memory hierarchy (i.e. caches), and so on.

like image 23
axtavt Avatar answered Oct 20 '22 17:10

axtavt