I was going through a document which talks about just-in-time compiler (JIT) optimization techniques for Java. One of them was "loop inversion". And the document says:
You replace a regular
while
loop with ado-while
loop. And thedo-while
loop is set within anif
clause. This replacement leads to two fewer jumps.
How does loop inversion work and how does it optimize our code path?
N.B.: It would be great if somebody can explain with an example of Java code and how JIT optimizes it to native code and why is it optimal in modern processors.
Loop Optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capabilities.
In computer science, loop inversion is a compiler optimization and loop transformation in which a while loop is replaced by an if block containing a do.. while loop. When used correctly, it may improve performance due to instruction pipelining.
Conversely, loop fusion (or loop jamming) is a compiler optimization and loop transformation which replaces multiple loops with a single one.
Some possible transformations on loops and loop nests include the following: Loop permutation changes the order of the loops in the nest. Index rewriting changes the way the loop indexes are expressed. Loop unrolling creates several copies of a loop body and modifies the loop indexes appropriately.
while (condition) { ... }
Workflow:
if (condition) do { ... } while (condition);
Workflow:
Comparing these two you can easily see that the latter may not do any jumps at all, provided that there is exactly one step through the loop, and generally the number of jumps will be one less than the number of iterations. The former will have to jump back to check the condition, only to jump out of the loop when the condition is false.
Jumps on modern pipelined CPU architectures can be quite expensive: as the CPU is finishing the execution of the checks before the jump, the instructions beyond that jump are already in the middle of the pipeline. All this processing must be discarded if the branch prediction fails. Further execution is delayed while the pipeline is being reprimed.
Explaining the mentioned branch prediction: for each kind of conditional jump the CPU has two instructions, each including a bet on the outcome. For example, you would put an instruction saying "jump if not zero, betting on not zero" at the end of a loop because the jump will have to be made on all iterations except the last one. That way the CPU starts pumping its pipeline with the instructions following the jump target instead of those following the jump instruction itself.
Please do not take this as an example of how to optimize at the source code level. That would be entirely misguided since, as already clear from your question, the transformation from the first form into the second is something the JIT compiler does as a matter of routine, completely on its own.
This can optimize a loop that is always executed at least once.
A regular while
loop will then always jump back to the start at least once and jump to the end once at the end. An example of a simple loop running once:
int i = 0; while (i++ < 1) { //do something }
A do-while
loop on the other hand will skip the first and last jump. Here is an equivalent loop to the one above, that will run without jumps:
int i = 0; if (i++ < 1) { do { //do something } while (i++ < 1); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With