Sub topic 10
Dead-code elimination and Blackhole
This chapter assumes you can write a basic @Benchmark method and know that the JIT compiler optimizes hot code at runtime. It covers one of the most common ways a benchmark ends up measuring nothing.
The problem
The JIT compiler removes work whose result is never used. This is called dead-code elimination. If a benchmark computes a value and throws it away, the compiler may delete the computation. The benchmark then reports a very fast time for code that never ran.
Look at this method:
@Benchmark
public void deadCode() {
Math.log(x); // result unused, the JIT may remove the call
}Math.log has no side effects, and nothing reads its result. The JIT is allowed to drop the call. You would see a score of a fraction of a nanosecond and might conclude that Math.log is almost free.
Fix 1: return the result
JMH consumes any value a @Benchmark method returns. The JIT can no longer prove that the result is unused, so it must do the work.
@Benchmark
public double returned() {
return Math.log(x);
}This is the simplest fix and the one to prefer when the method produces one result.
Fix 2: use a Blackhole
A method can return only one value. When it computes several, pass a Blackhole parameter and call consume on each result.
@Benchmark
public void blackhole(Blackhole bh) {
bh.consume(Math.log(x));
bh.consume(Math.sqrt(x));
}A Blackhole is a JMH object that accepts a value and prevents the JIT from treating it as unused. JMH injects it for you. You only need to declare it as a method parameter.
A full example
This class puts the three styles side by side, plus a fourth method that you will meet again in the next chapter.
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
@State(Scope.Thread)
public class FoldingBenchmark {
private double x = Math.PI;
@Benchmark
public void deadCode() {
Math.log(x); // result unused, the JIT may remove the call
}
@Benchmark
public double returned() {
return Math.log(x); // JMH consumes the returned value
}
@Benchmark
public void blackhole(Blackhole bh) {
bh.consume(Math.log(x)); // explicit consumption, useful for several results
}
@Benchmark
public double constantFolded() {
return Math.log(Math.PI); // constant input, the JIT may compute it at compile time
}
}Build the jar and run it with java -jar target/benchmarks.jar FoldingBenchmark. Expect deadCode to report a much lower time than returned and blackhole. The exact numbers depend on your machine and JVM.
The input x is a field of the state object, not a literal. That keeps the JIT from knowing its value in advance. constantFolded uses Math.PI directly, and the compiler may compute the result once at compile time. The next chapter explains that problem.
Common mistakes
- Forgetting to return a value from a loop-based benchmark. A summing loop whose total is never used can be removed entirely.
- Calling
consumeon only some results. Any result you skip can still be eliminated. - Returning a value and also passing a
Blackholefor the same result. This is harmless but redundant.
Exercise
A benchmark method computes both a sum and a maximum of an array, then returns nothing. How would you make sure neither computation is eliminated?
Show answer
Add a Blackhole parameter to the method and call bh.consume on the sum and on the maximum. Returning only one of them would leave the other open to dead-code elimination.