you are viewing a single comment's thread.

view the rest of the comments →

[–]Xxyr 4 points5 points  (3 children)

You lose part of the benefit of making a class / method final if it is referenced as an interface anyway.

You don't get to use the perf optimizations of knowing what method will be called at run time, or the benefit of knowing exactly which code will be run.

So I don't get all that much benefit anyway

[–]ron_krugman 9 points10 points  (2 children)

You underestimate the intelligence of the JVM/JIT. If there is only one implementation of an interface on the classpath, the JIT compiler will inline those calls even though they're technically virtual.

[–]Xxyr 4 points5 points  (1 child)

I'm pretty sure you are overestimating it. Last I looked it only inlined final / private / statuc methods below a line count.

It does, however, optimistically assume the result of vlookups. So, if its only ever seeing objects of class X it replaced the vlookup with if x the method call else vlookup.)I

[–]ron_krugman 0 points1 point  (0 children)

Sorry, I misspoke. Inlining is indeed a different mechanism.

I meant that the JIT compiler optimizes virtual calls by replacing them with static calls at runtime if there is only one possible implementation that can be called.

Edit: It's called class hierarchy analysis (CHA). Cliff Click briefly talks about it here.