Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: JIT method inlining

Tags:

java

inline

jit

When does Java JIT inline a method call? Is it based on #times the caller method is called (if yes, what would that number be?), or some other criteria (and what would that be?)

I've read that JIT can inline 'final' methods, but it also inlines nonfinal methods based on runtime statistics, so want to know what is that triggering criteria.

I guess the answers would differ based on JVM implementation, but maybe there's something common across all of them?

like image 674
shrini1000 Avatar asked Apr 09 '12 12:04

shrini1000


People also ask

What is method inlining in Java?

What Method Inlining Is? Basically, inlining is a way to optimize compiled source code at runtime by replacing the invocations of the most often executed methods with its bodies. Although there's compilation involved, it's not performed by the traditional javac compiler, but by the JVM itself.

What is inlining in programming?

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.

Does Java support inline function?

No, Java does not provide inline functions it is typically done by the JVM at execution time.

What is method inlining C#?

In essence, method inlining is when the compiler decides to replace your function call with the body of the function. The compiler does this to save the overhead of actually making a function call, which would involve pushing each parameter on to the stack, function prologue, function epilogue, etc. .


2 Answers

The short answer is whenever it wants.

Very often a JITC will inline small final or pseudo-final methods automatically, without first gathering any stats. This is because it's easy to see that the inlining actually saves code bytes vs coding the call (or at least that it's nearly a "wash").

Inlining truly non-final methods is not usually done unless stats suggest it's worthwhile, since inlined non-finals must be "guarded" somehow in case an unexpected subclass comes through.

As to the number of times something may be called before it's JITCed or inlined, that's highly variable, and is likely to vary even within a running JVM.

like image 84
Hot Licks Avatar answered Sep 18 '22 12:09

Hot Licks


the default inline threshold for a JVM running the server Hotspot compiler is 35 bytecodes.

Official docs

like image 31
NimChimpsky Avatar answered Sep 21 '22 12:09

NimChimpsky