-2

Ok, I was doing some testing, and instanceof seemed to be faster than polymorphism. So I found this very interesting answer that comproves my testing. instanceof is faster.

Changing one method of my app to instanceof if-else (7 classes), instead of polymorphism, reduced its load by almost 3%. (Over 50.000 calls per second to this method according to my custom profiler) - Also, please keep in mind, that the method in question, only returns a int, zero calculations, absolutely nothing special.

The quesion: Unfortunately, I couldn't find any real information of why. So, really deep technically speaking, how can a giant if else can be faster than a simple polymorphism/Visitor?

Source code of the benchmark: https://github.com/michaeldorner/instanceofBenchmark

Foreign
  • 385
  • 2
  • 20
  • 10
    what exactly is OO ? – Stultuske Aug 29 '18 at 07:20
  • "object orientated". In this scenario, I'm referring to the second option here: https://stackoverflow.com/revisions/26514984/10 – Foreign Aug 29 '18 at 07:22
  • http://www.theeggeadventure.com/wikimedia/index.php/InstanceOf_Performance – Akaino Aug 29 '18 at 07:24
  • Even Google doesn't find references to Object orientated. I'm not saying it 's not an existing concept, but so far you've only posted links to posts where people say they applied it, but never provide(d) an example of how it looks like. It might be difficult for people to help you with just this information. – Stultuske Aug 29 '18 at 07:25
  • 1
    @Stultuske: OP is asking about the benchmark itself (not as "example of OO", but the "OO" path in that very code, the speed of `testInstanceOf` vs the speed of `testOO`). (Yes, the question could have been clearer :P ) – Amadan Aug 29 '18 at 07:27
  • I'm not asking for help with my code. I've provided a link that contains the benchmark, multiple graphics and a github project with the benchmark source code. I'm asking for the technical explanation of WHY is instanceof faster. – Foreign Aug 29 '18 at 07:29
  • 1
    @Stultuske In this case (instanceof vs OO), OO simply means polymorphism, so the questions asks why checking the type of an object using `instanceof` is much faster than using polymorphism. I agree that it's not very clear at first. – sloth Aug 29 '18 at 07:29
  • I'm sorry if the question isn't clear enough. I'm not a native english speaker. – Foreign Aug 29 '18 at 07:31
  • @Amadan yes, but it is clear what "testInstanceOf" does, but what is "testOO" supposed to do? Are you actually suggesting we should try to determine the speed of a method by nothing at all but it's name? – Stultuske Aug 29 '18 at 07:31
  • Since you mention the performance on your application that do a lot of calls on this "logic". I would first try to understand why you need this kind of behavior. Checking the instance type of an object generally show a design problem. – AxelH Aug 29 '18 at 07:31
  • 1
    @Thiago I think the problem with your question is that's not immediatly clear what you're talking about when you say 'OO'. It took me a while to understand that you're talking about polymorphism. Maybe you should just add a little example code. – sloth Aug 29 '18 at 07:31
  • 1
    Here is the source code to the benchmark I'm referring to. https://github.com/michaeldorner/instanceofBenchmark (it is in the original post) – Foreign Aug 29 '18 at 07:33
  • The real answer will lie somewhere between https://shipilev.net/blog/2015/black-magic-method-dispatch/#_megamorphic_cases and the benchmark for instanceOf vs OO doing different things. I wonder what would happen if instanceOf called `doSomething()` instead. Might be worth trying out! Anyway, without `-perfasm` all answers are just guesses. – Petr Janeček Aug 29 '18 at 07:33
  • @Stultuske: As sloth says, it is not immediately clear, but if you read the link "OO approach" refers to dynamic dispatch via polymorphism. I don't suggest you understand it from `testOO` _name_, I suggest you understand it as I did, by reading the linked question, and/or the code of `testOO`, or simply accept my and sloth's assertion that we've investigated and concluded that that's what it's about. – Amadan Aug 29 '18 at 07:35
  • I've edited the question, hope its clearer now for future readers. – Foreign Aug 29 '18 at 07:41
  • @AxelH It is a networking code. When receiving a Packet object over the networking, it calls the abstract method "handle()" from the abstract class Packet. As I've stated in my question, I've switched to instanceof because it seems to be faster than polymorphism. - The question is: Why? – Foreign Aug 29 '18 at 07:55

1 Answers1

0

The JVM only inlines up to 2 possible implementations from a call site. This means if you have more than 2, the code won't be as optimised for some cases.

If instead, you do your only if/else, there is likely to be just one implementation to call and thus it can all be inlined.

Inlining is a key feature of many optimisation techniques.

In short, if you have a megamorphic call you can see a small but significant improvements by refactoring it if the code is hot enough. http://insightfullogic.com/2014/May/12/fast-and-megamorphic-what-influences-method-invoca/

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130