40

In C++ I can declare a method "inline" and the compiler is likely to inline it. As far as I understand there is no such keyword in Java.

Inlining is done if the JVM decides to do so? Can I influence this decision somehow?

8 Answers 8

55

A couple of the other answers have suggested that only final methods can be inlined - this is not true, as HotSpot is smart enough to be able to inline non-final methods so long as they haven't been overridden yet. When a class is loaded which overrides the method, it can undo its optimisation. Obviously making the method final mean that's never required...

Basically let the JVM do its job - it's likely to be a lot better at working out where to inline than you are.

Do you have a situation where you're convinced that the JVM isn't doing a good job? Assuming you're using HotSpot, have you tried using the server version instead of client? That can make a huge difference.

3
  • 30
    Actually HotSpot can inline methods (speculatively) even if it is overridden, and call the method virtually for objects that are not of the expected type. Server HotSpot will even inline two different versions of a method (bimorphic inlining). Jul 21, 2009 at 14:55
  • 3
    @Tom: Mind if I include that in the answer?
    – Jon Skeet
    Jul 21, 2009 at 15:00
  • 3
    well, I definitely had cases were hotspot did not inline and manual inlining lead to factor 5 improvement in performance of a core processing loop.
    – R.Moeller
    Jul 9, 2013 at 21:53
6

Although the java compiler can do inline (for short early-bound methods) the real inlining will be done by the JIT compiler. The JIT (HotSpot) compiler will be able to,even, inline virtual methods. The best way to interact with it is to write a simple and concise code. Most likely, code that uses Reflection will not allow for inlining.

Hope that helps.

2
  • 1
    Can you give any evidence for the Java compiler (as opposed to the JÍT compiler) inlining method calls? Oct 10, 2009 at 1:11
  • "Most likely, code that uses Reflection will not allow for inlining"This post seems to have it the other way around, is it?
    – MC Emperor
    Jul 18, 2017 at 6:47
5

'In C++ I can declare a method "inline" and the compiler will inline it'... or not. The compiler is free to make the function inline or not and you cannot really affect the result. It is only a hint to the compiler.

In Java there is no such thing, the compiler (and later the VM while performing optimizations) can decide to 'inline' the method.

Note that final methods have greater chances of being inlined (the compiler cannot inline non-final methods, as they may be overwritten in derived classes). With modern VM, a similar optimization can be made at runtime. The VM will flag the type (so it can perform type checks) and will inline the code. Only if the check fails, it will fall back into the original unoptimized polymorphic method call.

6
  • 2
    Important point! "inline" does not force the compiler to inline your method in C. Jul 21, 2009 at 13:26
  • Thx, you are right, I forgot that "inline" is a suggestion to the compiler!
    – CL23
    Jul 21, 2009 at 13:36
  • You can sometimes force the compiler to inline, but that's complier specific: stackoverflow.com/questions/934529/… Jul 21, 2009 at 22:27
  • I'm pretty sure that Java compilers can NOT decide to inline methods - only the JIT compiler can. Oct 10, 2009 at 1:10
  • 2
    "the compiler cannot inline non-final methods, as they may be overwritten in derived classes)" - completely false statement. The JIT compiler can and does inline non-final methods if it hasn't been overridden at run time yet.
    – Steve Kuo
    Jul 9, 2013 at 0:15
4

Inlining is more likely to happen if the method in question is:

  • short
  • final
  • not dependent on any long, non final methods

As these are the only circumstances where the JVM can be certain of the effects of the call.

3
  • 3
    final doesn't make any difference for HotSpot. Jul 21, 2009 at 14:53
  • @TomHawtin-tackline Interesting, could you elaborate?
    – MC Emperor
    Jul 18, 2017 at 6:48
  • @MCEmperor Subject to change, but it just checked whether any loaded classes overrides the method. Jul 18, 2017 at 22:08
3
class A {
    final int foo() { return 3; }
}

Given this class, any call to foo() can be replaced with the constant "3". Any Java1 virtual machine can do this, because the final keyword explicitly dictates that it isn't possible to have a subclass that overrides "int foo()".

Inlining the method provides the following benefits at the call site:

  • No method call
  • No dynamic dispatch
  • Possible to constant-fold the value, eg. "a.foo()+2" becomes 5 with no code executed at
    runtime.

In the past, programmers often inserted the final keyword for exactly this reason. Or to better facilitate inlining and increase execution speed, they would combine many smaller methods into one larger method. But in many ways, such techniques defeat the entire facility of modularization and reusability built into the programming language.

Modern JVM, like the Java HotSpot VM is able to inline the class without the final. keyword**.

(http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html)

2
  • A keyword(to advise the compiler) to make a function call inline must have made more sense given that for sake of readability you sometime break certain computation in several function while at the sametime you want those call to be made inline at runtime to save a lot of function-call overhead.
    – mawia
    Feb 4, 2013 at 14:07
  • Answer by @David Rodriguez below make sense. If inline keyword is meant to be ONLY a HINT to the compiler, then it is better be left to the compiler and trust it, that it will inline the call wherever possible without our advice.
    – mawia
    Feb 4, 2013 at 14:15
2

Read this for Inlining behavior. http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html

It says Final methods can be Inlined but not always.

1
  • 1
    The fact that that article implies interpretation as the norm - "there are just-in-time compilers appearing at this writing" suggests it's hardly up to date. VMs have come a long way.
    – Jon Skeet
    Jul 21, 2009 at 13:21
1

Yes, if the JVM decides to do it, it can. Ways to influence include setting the method as static or as final.

Of course, the most important thing about it is that the structure of the method needs to be inline friendly. Short helps, but most importantly it needs to only use its local variables and its parameters, no fields, and minimal method calls to other methods in the same class.

However you should not look to do such optimizations prematurely, you could actually be making things worse (because you could be short-circuiting other potential optimizations). The JVM will sometimes realize that a method can be inlined without these hints.

1

When comparing a normal function and final function(which is said to be inline by JVM), I have seen that there is no performance improvement between them. Maybe overhead of function call is already very low.

Note: I used box blurring algorithm for evaluating performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.