Inlining in Java

2019-01-04 02:26发布

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条回答
倾城 Initia
2楼-- · 2019-01-04 03:07

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-04 03:08

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-04 03:15
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)

查看更多
smile是对你的礼貌
5楼-- · 2019-01-04 03:16

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.

查看更多
该账号已被封号
6楼-- · 2019-01-04 03:18

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.

查看更多
够拽才男人
7楼-- · 2019-01-04 03:20

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

It says Final methods can be Inlined but not always.

查看更多
登录 后发表回答