I was looking through new intrinsic methods in Java 9 and found that now the method Class.cast is intrinsic too along with Class.isInstance.
I did simple benchmark to check that:
@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, batchSize = 1000)
@Measurement(iterations = 20, time = 1, batchSize = 1000)
public class Casting {
public Object msg;
public Class<String> type;
@Setup
public void setup(BenchmarkParams params) {
type = String.class;
msg = "123";
}
@Benchmark
public boolean isInstanceMethod() {
return type.isInstance(msg);
}
@Benchmark
public boolean isInstanceMethodExplicit() {
return String.class.isInstance(msg);
}
@Benchmark
public boolean isInstanceRegular() {
return msg instanceof String;
}
@Benchmark
public String castMethod() {
return type.cast(msg);
}
@Benchmark
public String castMethodExplicit() {
return String.class.cast(msg);
}
@Benchmark
public String castRegular() {
return (String) msg;
}
}
Results:
Benchmark Mode Cnt Score Error Units
Casting.castMethod thrpt 20 254604.793 ± 3863.859 ops/s
Casting.castMethodExplicit thrpt 20 336046.787 ± 10059.986 ops/s
Casting.castRegular thrpt 20 344369.229 ± 4855.492 ops/s
Casting.isInstanceMethod thrpt 20 325867.697 ± 6511.731 ops/s
Casting.isInstanceMethodExplicit thrpt 20 415387.363 ± 2993.788 ops/s
Casting.isInstanceRegular thrpt 20 396613.371 ± 16799.378 ops/s
Env:
# JMH version: 1.19
# VM version: JDK 9, VM 9+181
# VM invoker: /usr/lib/jvm/java-9-oracle/bin/java
The result seems a bit unexpected. Both Class.cast and Class.isInstance are slower when invoking them via class variable. My understanding was that intrinsic should make both this method as fast as alternatives.
Does variable breaks intrinsic optimization? Or this overhead is expected?