Java specifications - what are the rules for "proper" optimization

I understand that optimization is valid if it does not have an observable effect that contradicts JLS. So, for example, the JIT compiler can optimize the "do nothing" code in the inner loop.

But I do not find a final statement about this.

Does anyone know about the final expression (for example, in JLS or a document with a similar status), when is the actual Java optimization (for example, executed by the native code compiler)?

+4
source share
3 answers

JLS and the JVM specification indicate that the behavior of any Java statement (or for the JVM specification, how byte codes work, etc.), but they do not say anything about how this should happen. In both documents, it is understood that any implementation that correctly implements the specified abstract behavior is considered a compatible Java implementation. The basic idea of ​​having an abstract standard is to indicate which observable behaviors should be shared across all implementations, without going into the details of what these behaviors do. For this reason, implementations and their optimizers are allowed to do what, in their opinion, is necessary and correct for the code to execute if they do not deviate from the specified semantics.

Hope this helps!

+3
source

Compiler optimization is valid until the code behaves differently than the standard. This applies to all languages.

I do not think that this fact should be specifically indicated, since the only requirement for a standards-compliant compiler is that it behaves according to the standard. Optimization, which does not change its apparent behavior, obviously does not change whether the standards comply with the standards or not.

+1
source

For example, the String pool , as mentioned here, is a form of optimization. Similar concepts exist for afaik for small values ​​of Integer and Long.

You may find more interesting answers here and an explanation of the whole pool here.

+1
source

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


All Articles