I am working on an application and one design approach involves extremely heavy use of the instanceof
operator. While I know that OO design generally tries to avoid using instanceof
, that is a different story and this question is purely related to performance. I was wondering if there is any performance impact? Is is just as fast as ==
?
For example, I have a base class with 10 subclasses. In a single function that takes the base class, I do checks for if the class is an instance of the subclass and carry out some routine.
One of the other ways I thought of solving it was to use a "type id" integer primitive instead, and use a bitmask to represent categories of the subclasses, and then just do a bit mask comparison of the subclasses "type id" to a constant mask representing the category.
Is instanceof
somehow optimized by the JVM to be faster than that? I want to stick to Java but the performance of the app is critical. It would be cool if someone that has been down this road before could offer some advice. Am I nitpicking too much or focusing on the wrong thing to optimize?
Approach
I wrote a benchmark program to evaluate different implementations:
instanceof
implementation (as reference)@Override
a test methodgetClass() == _.class
implementationI used jmh to run the benchmark with 100 warmup calls, 1000 iterations under measuring, and with 10 forks. So each option was measured with 10 000 times, which takes 12:18:57 to run the whole benchmark on my MacBook Pro with macOS 10.12.4 and Java 1.8. The benchmark measures the average time of each option. For more details see my implementation on GitHub.
For the sake of completeness: There is a previous version of this answer and my benchmark.
Results
tl;dr
In Java 1.8
instanceof
is the fastest approach, althoughgetClass()
is very close.The items which will determine the performance impact are:
I created a microbenchmark for four different methods of dispatch. The results from Solaris are as follows, with the smaller number being faster:
Demian and Paul mention a good point; however, the placement of the code to execute really depends on how you want to use the data...
I'm a big fan of small data objects that can be used in many ways. If you follow the override (polymorphic) approach, your objects can only be used "one way".
This is where patterns come in...
You can use double-dispatch (as in the visitor pattern) to ask each object to "call you" passing itself -- this will resolve the type of the object. However (again) you'll need a class that can "do stuff" with all of the possible subtypes.
I prefer to use a strategy pattern, where you can register strategies for each subtype you want to handle. Something like the following. Note that this only helps for exact type matches, but has the advantage that it's extensible - third-party contributors can add their own types and handlers. (This is good for dynamic frameworks like OSGi, where new bundles can be added)
Hopefully this will inspire some other ideas...
You're focusing on the wrong thing. The difference between instanceof and any other method for checking the same thing would probably not even be measurable. If performance is critical then Java is probably the wrong language. The major reason being that you can't control when the VM decides it wants to go collect garbage, which can take the CPU to 100% for several seconds in a large program (MagicDraw 10 was great for that). Unless you are in control of every computer this program will run on you can't guarantee which version of JVM it will be on, and many of the older ones had major speed issues. If it's a small app you may be ok with Java, but if you are constantly reading and discarding data then you will notice when the GC kicks in.
InstanceOf is a warning of poor Object Oriented design.
Current JVMs do mean the instanceOf is not much of a performance worry in itself. If you are finding yourself using it a lot, especially for core functionality, it is probably time to look at the design. The performance (and simplicity/maintainability) gains of refactoring to a better design will greatly outweigh any actual processor cycles spent on the actual instanceOf call.
To give a very small simplistic programming example.
Is a poor architecture a better choice would have been to have SomeObject be the parent class of two child classes where each child class overrides a method (doSomething) so the code would look as such:
In modern Java version the instanceof operator is faster as a simple method call. This means:
is faster as:
Another thing is if you need to cascade many instanceof. Then a switch that only call once getType() is faster.