开发者

Java specifications - what are the rules for a "valid" optimization

My understanding is that an optimization is valid if it has no observable effect that contradicts the JLS. So开发者_如何学JAVA for example, the JIT compiler can optimize away "do nothing" code in an inner loop.

But I don't recalling a definitive statement to this effect.

Does anyone know of a definitive statement (i.e. in the JLS or a document of similar standing) of when a Java optimization (e.g. performed by the native code compiler) is valid?


The JLS and the JVM spec both specify what the behavior of any Java statement is (or for the JVM spec how bytecodes work, etc.), but they say nothing about how that behavior is to occur. It's implicit in the two documents that any implementation that correctly implements the abstract behavior specified is considered a compliant Java implementation. The main idea behind having an abstract standard is to specify what observable behaviors must be shared across all implementations without going into the details of what makes those behaviors occur. For this reason, implementations and their optimizers are allowed to do whatever they feel is necessary and proper to make the code run, so long as they don't deviate from the specified semantics.

Hope this helps!


A compiler optimization is valid as long as it doesn't make code behave differently from the standard. This applies for all languages.

I don't think there's a need to specifically state this fact, as the only requirement for a standards compliant compiler is that it behaves as the standard describes. An optimization that doesn't change its apparent behavior obviously doesn't change whether it's standards compliant or not.


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

Maybe you find more interesting answers here and an explanation of the Integer pool here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜