- Mastering High Performance with Kotlin
- Igor Kucherenko
- 118字
- 2021-06-25 20:55:26
Dead Code Elimination
Another downfall of benchmarks is Dead Code Elimination (DCE). The JVM reduces computations that are redundant or eliminates them completely. Let's come back to our first implementation of the testMethod() method:
@Benchmark
public void testMethod() {
int a = 3;
int b = 4;
int c = a + b;
}
In this example, the last line will be eliminated, but it's a significant part of our benchmark. The JMH provides the essential infrastructure to fight this issue. We can just return the result of computation as follows:
@Benchmark
public int testMethod() {
int a = 3;
int b = 4;
return a + b;
}
The returned result is implicitly consumed by black holes.