Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the JVM have a maximum inline depth?

Tags:

java

jvm

jit

java has an argument -XX:MaxInlineLevel (with a default value of 9) which controls the maximum number of nested calls to inline. Why is there any such limit? Why aren't the usual heuristics based on frequency and code size sufficient for the JVM to decide for itself how deeply to inline?

(this is prompted by JitWatch showing me that a deeply nested Guava checkArgument call was not being inlined due to depth)

like image 553
Ryan Gabbard Avatar asked Sep 10 '15 13:09

Ryan Gabbard


1 Answers

Some significant searching uncovers this interesting little fragment (I actually got as far as page 4 of the Google search):

    if (inline_depth() > MaxInlineLevel) {
        return "inlining too deep";
    }
    if (method() == callee_method
            && inline_depth() > MaxRecursiveInlineLevel) {
        return "recursively inlining too deep";
    }

Which suggest that the MaxInlineLevel is as expected a hard limit to how deep you go before you stop inlining. It also suggests that the MaxRecursiveInlineLevel refers only to direct recursive calls, not mutal recursive calls such as foo() calls bar() which calls foo().

So I think I was right in my guess comment - MaxInlineLevel is to protect against mutual recursion because to detect that you would need to keep references to the full depth of the inlining call stack.

MaxInlineResursionLevel controls foo() calls foo() inlining.

Note that the referenced code may not be a real JVM.

Comments by @apangin locates a more modern version of hotspot from Open JDK 8 suggest that it is nowadays no longer quite as simple as that. It looks like the full stack is searched for recursive calls so mutual recursion may also now be blocked from going past MaxRecursiveInlineLevel.

like image 191
OldCurmudgeon Avatar answered Oct 18 '22 09:10

OldCurmudgeon