Performance of instanceof with interfaces is multiple times slower than with classes

John Rose john.r.rose at oracle.com
Fri Jul 17 23:13:24 UTC 2020


On Jul 15, 2020, at 2:08 AM, Christoph Dreis <christoph.dreis at freenet.de> wrote:
> 
> Could you enlighten me what the cause for this is and maybe point me to the code where this is done?

A microbenchmark like that doesn’t demonstrate much.

The JVM optimizations which are designed for real applications may or may not apply to this code.  They will certainly apply differently in real-life application execution.  In real life, class hierarchies are more complex, and so the JVM expects to do more work telling types apart; at that point differences between single inheritance and multiple inheritance requires differing algorithms with different complexities and costs.  Also, in real life, inputs to such hot paths are not just all one type (as in your micro), which is another reason you get into a different set of costs and complexities.

If this were related to a real-world application, or a realistic benchmark, I might get assembly code for the hot paths in the two methods, and see what the CPU is doing that makes a difference in speed.  Then I might meditate on what decisions the JVM made to choose that code, and see if there is an improvement possible.

You ask “point me to the code” which suggests you haven’t looked at the code yet.  When you do (and I hope you do!) you will find that there are many, many algorithms and decisions in the JVM that affect the treatment of runtime type tests.  You could start by grepping around for “subtype” (case insensitive) in the *.[ch]pp and *.ad files under src/hotspot.

> Is this maybe even a bug/regression? Can we maybe do something to improve the interface case?

I will stick my neck out to say that microbenchmarks alone by definition never demonstrate performance regressions or bugs.

Yes, we can always do something to improve it.  Improving this micro could involve attempting to treat trivial interface hierarchies like trivial class hierarchies:  But who would this benefit?  Our investment of effort is surely driven by a hope to spend limited effort to get the most benefit on real applications.

— John



More information about the hotspot-runtime-dev mailing list