Currently, if running with -XX:-OmitStackTraceInFastThrow, C2 has no possibility to create implicit exceptions like AIOOBE, NullPointerExceptions, etc. in compiled code with the effect that these methods will always be deoptimized and reexecuted in the interpreter if such an exception is happening.
If such implicit exceptions are used for normal control flow, that can have a dramatic impact on performance. A prominent example for such code is Tomcat's HttpParser::isAlpha() method[1]:
public static boolean isAlpha(int c) {
try {
return IS_ALPHA[c];
} catch (ArrayIndexOutOfBoundsException ex) {
return false;
}
}
Instead of deoptimizing and resorting to the interpreter, we can generate code which allocates and initializes the corresponding exceptions right in compiled code.
A prototype which implements this idea shows a ten-times performance improvement for the above code:
-XX:-OmitStackTraceInFastThrow
Benchmark (exceptionProbability) Mode Cnt Score Error Units
ImplicitExceptions.bench 0.0 avgt 5 1.430 ± 0.353 ns/op
ImplicitExceptions.bench 0.33 avgt 5 3563.038 ± 77.358 ns/op
ImplicitExceptions.bench 0.66 avgt 5 8609.693 ± 1205.104 ns/op
ImplicitExceptions.bench 1.00 avgt 5 12842.401 ± 1022.728 ns/op
-XX:-OmitStackTraceInFastThrow -XX:+OptimizeImplicitExceptions
Benchmark (exceptionProbability) Mode Cnt Score Error Units
ImplicitExceptions.bench 0.0 avgt 5 1.432 ± 0.352 ns/op
ImplicitExceptions.bench 0.33 avgt 5 355.723 ± 16.641 ns/op
ImplicitExceptions.bench 0.66 avgt 5 887.068 ± 166.728 ns/op
ImplicitExceptions.bench 1.00 avgt 5 1274.418 ± 88.235 ns/op
[1] https://github.com/apache/tomcat/blob/26ba86cdbd40ca718e43b82e62b3eb49d004c3d6/java/org/apache/tomcat/util/http/parser/HttpParser.java#L266-L274