Java, Assertions and the JIT -


i'm trying reason how jit of hotspot reasons. i'm interested in latest compilation stage (c2 compiler). jit in java rely on assertions optimisations? if case, imagine there examples code run faster assertions enabled.

for example, in piece of code this:

static int getsumoffirstthree(int[] array) {    assert(array.length >= 3);    return array[0] + array[1] + array[2]; } 
  • will jit, when assertions enabled, clever enough eliminate bounds checks on array accesses?
  • alternatively, there other cases can think of (practical or not) assertions improve native code jit compile?

in case, there multiple bounds checks made, , possible jit can coalesce them 1 check made, assertion doesn't avoid need make check.

assertions prevent optimisations inlining method larger , size factor in determining whether inline method. typically inlining improves performance in cases doesn't can cause l0 or l1 cpu caches become inefficient due larger code being generated.

an example of assertion can improve performance this.

boolean assertionon = false; assert assertionon = true; if (assertionon) {    assumedataisgood(); // due checks elsewhere } else {    expensivecheckthatdatamightnotbegood(); } 

this perhaps anti-pattern using assertions, going cheaper assertions on intent.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -