Java, assertion and JIT

I tried to infer the JIT reason of hotspot I am most interested in the latest compilation phase (C2 compiler) Does JIT in Java rely on assertions for optimization? If this is the case, I can imagine some examples that can run code faster with assertion enabled

For example, in such a piece of code:

static int getSumOfFirstThree(int[] array) {
   assert(array.length >= 3);
   return array[0] + array[1] + array[2];
}

>When assertions are enabled, is the JIT smart enough to eliminate boundary checking for array access? > Or, can you think of other situations (actual or not) where assertions actually improve JIT compiled native code?

Solution

In this case, multiple boundary checks are performed, and the JIT can merge them so that they are checked only once, but assertions do not avoid checking

Assertions prevent optimization such as inlining because methods are large and size is a factor in determining whether to inline methods In general, inlining can improve performance, but in some cases, it will not make l0 or L1 CPU cache inefficient due to generating larger code

An example of how assertions can improve performance is this

boolean assertionOn = false;
assert assertionOn = true;
if (assertionOn) {
   assumeDataIsGood(); // due to checks elsewhere
} else {
   expensiveCheckThatDataMightNotBeGood();
}

This may be an anti - pattern that uses assertions, but it's cheaper to assert through intent

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>