16

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?

shrini1000
  • 7,038
  • 12
  • 59
  • 99
  • 1
    no easy answer but some links that might help - http://www.azulsystems.com/blog/cliff/2011-04-04-fixing-the-inlining-problem https://wikis.oracle.com/display/HotSpotInternals/Inlining https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques – Matt Apr 09 '12 at 12:40
  • Somewhat related article http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html – Steve Kuo Apr 09 '12 at 16:17

3 Answers3

19

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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Does 'small' depend on #lines? If yes, typically up to how many would mean 'small'? – shrini1000 Apr 09 '12 at 12:22
  • 4
    Not number of lines. Rather, estimated number of bytes of generated code (though the estimation may be quite crude). A JITC doesn't see "lines", but rather sees "bytecodes". – Hot Licks Apr 09 '12 at 12:25
  • 2
    And when JITCing a larger method, a JITC will generally have a "budget" for the generated size, and will pick stuff to inline until that "budget" is filled. – Hot Licks Apr 09 '12 at 12:26
  • 2
    As to how big of a method would be "small", generally any method that simply sets or returns a value (a "setter" or "getter") will be automatically inlined, as will methods that do simple computations. (So "setters" and "getters" are "free" and their presence adds no execution overhead to the JITCed code.) A JITC might or might not consider a method with a simple if/then/else to be "small. – Hot Licks Apr 09 '12 at 12:31
  • Thanks for these great details. Btw, what is a 'pseudo-final' method? – shrini1000 Apr 09 '12 at 12:33
  • Aside from having "final" on the class or method, a method can be private, and there are a few other circumstances where the JITC can in confidence assume that the method will not be overridden. I don't recall all of the different situations. – Hot Licks Apr 09 '12 at 12:35
  • 1
    final has no effect on if a method is inlined or not. The statement that non-final must be guarded is also incorrect as the JVM knows of all classes that are loaded. If a method is never overridden, then the JVM doesn't need to treat it as virtual. Thus it doesn't care if it's private or final. See this excellent Brian Goetz article on final ibm.com/developerworks/java/library/j-jtp1029/index.html – Steve Kuo Apr 09 '12 at 16:15
  • 2
    @SteveKuo -- How little you know. If an overriding class has not YET been loaded then yes, one can inline the method, after marking the containing JITCed method to be invalidated should the method be overriden. But if JITCs avoided inlining methods that had been overridden somewhere it would miss a lot of good optimizations. So if stats show that the objects coming in are always (or even usually) of a non-overriding variety a JITC may still choose to inline the method, at the expense of adding a guard somewhere. (I worked on one of the IBM JITCs.) – Hot Licks Apr 09 '12 at 17:29
8

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

Official docs

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
2

Typically JIT only inlines "small" methods by default. Other than that it's completely dependent on the implementation.

Tadas S
  • 1,955
  • 19
  • 33