|
|
|
|
|
by hyperpape
398 days ago
|
|
Even in older versions, if the compiler can see that there are no side-effects, it is free to remove the loop and simply return the value from the first iteration. I'm actually pretty curious to see what this method does on versions that don't have the optimization to treat hashCodes as quasi-final. A quick test using Java 17 shows it's not being optimized away _completely_, but it's taking...~1 ns per iteration, which is not enough to compute a hash code. Edit: I'm being silly. It will just compute the hashcode the first time, and then repeatedly check that it's cached and return it. So the JIT doesn't have to do any _real_ work to make this skip the hash code calculation. So most likely, the effective code is: computeHashCode();
for (int i = 0; i < 10000; i++) {
if (false) { // pretend this wouldn't have dead code elimination, and the boolean is actually checked
computeHashCode();
}
}
|
|