What is method inlining?

31,893

Solution 1

Inlining is an optimization performed by the Java Just-In-Time compiler.

If you have a method:

public int addPlusOne(int a, int b) {
  return a + b + 1;
}

which you call like this:

public void testAddPlusOne() {
  int v1 = addPlusOne(2, 5);
  int v2 = addPlusOne(7, 13);

  // do something with v1, v2
}

the compiler may decide to replace your function call with the body of the function, so the result would effectively look like this:

public void testAddPlusOne() {
  int v1 = 2 + 5 + 1;
  int v2 = 7 + 13 + 1

  // do something with v1, v2
}

The compiler does this to save the overhead of actually making a function call, which would involve pushing each parameter on to the stack.

This can clearly only be done for non-virtual functions. Consider what would happen if the method was overriden in a sub class and the type of the object containing the method isn't known until runtime...how would the compiler know what code to copy: the base class's method body or the sub class's method body? Since all methods are virtual by default in Java, you can explicitly mark those which cannot be overriden as final (or put them into a final class). This will help the compiler figure out that method will never be overriden, and it is safe to inline. (Note that the compiler can sometimes make this determination for non-final methods as well.)

Also, note the word may in the quote. Final methods aren't guaranteed to be inlineable. There are various ways you can guarantee a method isn't capable of being inlined, but no way to force the compiler to inline. It will almost always know better than you anyway when inlining will help vs. hurt the speed of the resulting code.

See wikipedia for a good overview of benefits and problems.

Solution 2

Let's say you have a class that looks like this:

public class Demo {
    public void method() {
        // call printMessage
        printMessage();
    }

    public void printMessage() {
        System.out.println("Hello World");
    }
}

The call to printMessage could be "inlined" in the following way:

public class Demo {
    public void method() {
        // call printMessage
        System.out.println("Hello World"); // <-- inlined
    }

    public void printMessage() {
        System.out.println("Hello World");
    }
}

(This is actually not done on the level of Java (not even on bytecode level) but during JIT-compilation, but the example above illustrates the concept of inlining.)

Now consider what would happen if the printMessage method was overloaded by another class, like this:

class SubDemo extends Demo {
    public void printMessage() {
        System.out.println("Something else");
    }
}

Now if the compiler inlined the call to Demo.printMessage it would be stuck with System.out.println("Hello World"); which would be wrong in case the object was actually an instance of SubDemo.

However, if the method was declared final this would not under any circumstances be the case. If the method is "final" it means that it can never be overridden with a new definition, thus, it is safe to inline it!

Solution 3

Calling a function is not free. The machine must maintain a stack frame so that it can return to the calling section of code when the called function is complete. Maintaining the stack (including passing function parameters on this stack) takes time.

When a function is in-lined, the compiler replaces the call to the function with the function's code so that one can avoid the performance penalty of a function call at run-time. This is one of the classic trade-offs in programming: the run-time code gets a little bigger (takes up more memory), but it runs a little faster.

Share:
31,893

Related videos on Youtube

Tarik
Author by

Tarik

I am a software engineer with interests in different technologies.

Updated on October 14, 2020

Comments

  • Tarik
    Tarik over 3 years

    I've been trying to understand what that really means :

    inline function

    In C++, a member function defined in the class declaration. (2) A function call that the compiler replaces with the actual code for the function. The keyword inline can be used to hint to the compiler to perform inline expansion of the body of a member or nonmember function.

    inline

    To replace a function call with a copy of the function's code during compilation.

    For example it is written something like :

    When a method is final, it may be inlined.

    Here : http://www.roseindia.net/javatutorials/final_methods.shtml

    Can you give me an example or something or basically help me to understand what "it may be inlined" means.

    Thanks.

  • amara
    amara over 13 years
    Good answer, but as a note: If you have a non-final method that you don't override, a good JIT can figure that out and inline it anyway. If you then load a class that does override it, it can undo the inlining.
  • amara
    amara over 13 years
    Also good answer, but as a note copied from another answer: If you have a non-final method that you don't override, a good JIT can figure that out and inline it anyway. If you then load a class that does override it, it can undo the inlining.
  • Tom Tresansky
    Tom Tresansky over 13 years
    Good point, I added it to the answer body.
  • Tarik
    Tarik over 13 years
    It is very explanatory, thanks for this great answer.
  • Tarik
    Tarik over 13 years
    Thanks. At the first paragraph, you are really right.
  • Tarik
    Tarik over 13 years
    It is very explanatory, thanks for this great answer.