Normally, Java optimizes the virtual calls based on the number of implementations encountered on a given call side. This can be easily seen in the results of my benchmark, when you look at myCode
, which is a trivial method returning a stored int
. There's a trivial
static abstract class Base {
abstract int myCode();
}
with a couple of identical implementation like
static class A extends Base {
@Override int myCode() {
return n;
}
@Override public int hashCode() {
return n;
}
private final int n = nextInt();
}
With increasing number of implementations, the timing of the method call grows from 0.4 ns through 1.2 ns for two implementations to 11.6 ns and then grows slowly. When the JVM has seen multiple implementation, i.e., with preload=true
the timings differ slightly (because of an instanceof
test needed).
So far it's all clear, however, the hashCode
behaves rather differently. Especially, it's 8-10 times slower in three cases. Any idea why?
UPDATE
I was curious if the poor hashCode
could be helped by dispatching manually, and it could a lot.
A couple of branches did the job perfectly:
if (o instanceof A) {
result += ((A) o).hashCode();
} else if (o instanceof B) {
result += ((B) o).hashCode();
} else if (o instanceof C) {
result += ((C) o).hashCode();
} else if (o instanceof D) {
result += ((D) o).hashCode();
} else { // Actually impossible, but let's play it safe.
result += o.hashCode();
}
Note that the compiler avoids such optimizations for more than two implementation as most method calls are much more expensive than a simple field load and the gain would be small compared to the code bloat.
The original question "Why doesn't JIT optimize the hashCode
like other methods" remains and hashCode2
proofs that it indeed could.
UPDATE 2
It looks like bestsss is right, at least with this note
calling hashCode() of any class extending Base is the same as calling Object.hashCode() and this is how it compiles in the bytecode, if you add an explicit hashCode in Base that would limit the potential call targets invoking Base.hashCode().
I'm not completely sure about what's going on, but declaring Base.hashCode()
makes a hashCode
competitive again.
UPDATE 3
OK, providing a concrete implementation of Base#hashCode
helps, however, the JIT must know that it never gets called, as all subclasses defined their own (unless another subclass gets loaded, which can lead to a deoptimization, but this is nothing new for the JIT).
So it looks like a missed optimization chance #1.
Providing an abstract implementation of Base#hashCode
works the same. This makes sense, as it provides ensures that no further lookup is needed as each subclass must provide its own (they can't simply inherit from their grandparent).
Still for more than two implementations, myCode
is so much faster, that the compiler must be doing something subobtimal. Maybe a missed optimization chance #2?
See Question&Answers more detail:
os