I've got a very simple micro-benchmark
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
public class Test {
List<Integer> list = new Random().ints(100_000).boxed().collect(toList());
@Benchmark public int mapToInt() {
return list.stream().mapToInt(x -> x * x).sum();
}
}
When I run it, I always get a result where the first warmup run is much faster than the next runs:
# Warmup Iteration 1: 171.596 us/op
# Warmup Iteration 2: 689.337 us/op
....
Iteration 1: 677.625 us/op
....
Command line:
java -jar target/benchmarks.jar .*Test.* -wi 5 -w 1000ms -i 10 -r 1000ms -t 1 -f 5 -tu us
Playing with the number of forks or threads does not seem to make a difference.
So it looks like some optimisation gets reverted but I can't find what it is.
Is the degraded performance due to an issue with my benchmark or is this de-optimisation representative of what would happen in a real application?
Note: this is a follow-up of Is there any advantage of calling map after mapToInt, where ever required?