Unable to understand compiler optimization statement

I'm interested in optimizing at VM runtime and at compile time. I had the idea that optimization is the most efficient and easiest at compile time.

However, in certain circumstances, my thought seems false. This is seen in a statement by Steve Yeggie quoted by Daniel

[O] ptimization is often simpler when executed at runtime by a smart virtual machine.

Why is optimization easier at run-time at VM than at compile time?

+4
source share
7 answers

Short answer:. Since it’s easier to identify and analyze hot spots at run time, the parts of your program that use the most time.

Long answer:

If you run the code in interpreted mode, the virtual machine can count how often and for how long different parts of the code are used. These parts can be optimized better.

Take the nested if-then-else clauses. Fewer logical checks require shorter runtimes. If you optimize the path for the part that runs more often, you can improve the overall execution time.

Another thing is that at runtime you can make assumptions that are impossible at compile time. Java-VM, for example, is built into virtual methods in server mode - while only one class is loaded that implements this method. This would be unsafe if it were done at compile time. The JVM disables the code again if another class is loaded, but often this never happens.

Also at runtime, it is more known about the machine on which the program is running. If you have a machine with a lot of registers, you can use them. Again, this is unsafe if you do this at compile time.

One thing can be said: runtime optimization also has disadvantages. Most importantly: time for optimization is added to the runtime of the program. It is also more complicated because you have to compile parts of the program and execute them. Errors in the virtual machine are critical. Think of a compiler that sometimes crashes - you can compile again, and everything is fine. Sometimes a virtual machine crash sometimes means that sometimes your program crashes. Not good.

As a conclusion: you can do each optimization at runtime, which is possible at compile time ... and a few more. You have additional information about the program, its startup paths and the machine in which the program runs. But you must consider the time required to complete the optimization. It is also more difficult to do at run time, and errors are more relevant than at compile time.

+4
source

Because there is more information available at runtime. For example, accurate data is known about the processor, operating system, and other environments. This information affects optimization.

+2
source

The VM has the full code of the program, and the compiler often has only partial code due to the separate translation of different translation units. Therefore, VM has more data to analyze.

+2
source

Because at run time, you have additional information: how the machine works, the limits of your process memory, and most importantly, what code is executed and how often.

These features allow you to perform runtime optimizations that you simply cannot do at compile time.

+1
source

VM can collect statistics for optimization, as well as a database for your use.

+1
source

Constantly keeping statistics and checking invariants, also the overhead of the execution time of compiled or interpreted fragments. If you can’t optimize quickly and well enough, don’t worry. I do not think it is easier to achieve any better results by doing this time rather than compilation time. I think it's even harder to consider the complexity of a good implementation.

It seems like a common misconception about good optimization compilers creating a better build than people, a smart enough virtual machine is probably too smart to run faster.

+1
source

Something that needs to be recognized is that the concept of VM does not optimize runtime, it is the fact that many virtual machines do not throw out the source metadata of the program and do not have built-in functions for reflection. A more appropriate term to use would be "Runtime Library", which can improve optimizations than static optimizations; this applies to non-VM languages ​​such as C.

0
source

Source: https://habr.com/ru/post/1286803/


All Articles