June 2018
Intermediate to advanced
316 pages
6h 34m
English
Constant folding is the flip side of the DCE. If your calculation is based on constants and the result is often exactly the same, the JVM will detect this and replace the calculation with the result. To make sure this doesn't happen, we can move the computation outside the internal JMH loop. Constant folding can be prevented by always reading the inputs from non-final instance fields of a state object. In this case, the result will be based on fields of the state object:
@Benchmarkpublic int testMethod() { int a = 3; int b = 4; return a + b;}
So, in the preceding code, the JVM can detect that the result value is based on two constants, a and b, and can replace our code with this:
public class MyBenchmark { @Benchmark public ...Read now
Unlock full access