Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inlining in Java

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?

like image 535
CL23 Avatar asked Jul 21 '09 13:07

CL23


People also ask

What is inlining in code?

Inlining is the process of replacing a subroutine or function call at the call site with the body of the subroutine or function being called. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What is inlining in compiler?

In computing, inline expansion, or inlining, is a compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the size of the final program.

What is inline function in Java give example?

If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

How do you create an inline in Java?

No, there is no inline function in java. Yes, you can use a public static method anywhere in the code when placed in a public class. The java compiler may do inline expansion on a static or final method, but that is not guaranteed.

What is method inlining in Java?

In computing, inline expansion, or inlining, is a compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the size of the final program. As a Java developer, you generally don't have to worry about method inlining.

What is inlining in JVM?

Norman Maurer explains at his blog JVM and JIT inline functionality like that Inlining is a technique that will basically just "inline" one method in another and so get rid of a method invocation. The JIT automatically detects "hot" methods and try to inline them for you.

What is the default value of JIT inlining in Java?

The default value depends on the platform – for 64-bit Linux, it's 325. The JIT inlines static, private, or final methods in general. And while public methods are also candidates for inlining, not every public method will necessarily be inlined. The JVM needs to determine that there's only a single implementation of such a method.

Why are public methods not inlined in Java?

And while public methods are also candidates for inlining, not every public method will necessarily be inlined. The JVM needs to determine that there's only a single implementation of such a method. Any additional subclass would prevent inlining and the performance will inevitably decrease.


2 Answers

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.

like image 144
Jon Skeet Avatar answered Oct 02 '22 20:10

Jon Skeet


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.

like image 31
Shimi Bandiel Avatar answered Oct 02 '22 20:10

Shimi Bandiel