Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is method inlining? [duplicate]

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.

like image 599
Tarik Avatar asked Oct 13 '10 14:10

Tarik


People also ask

What is method inlining?

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 does inlining a function do?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. 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 inlining in C#?

Inlining is the process by which code is copied from one function (the inlinee) directly into the body of another function (the inliner). The reason for this is to save the overhead of a method call and the associated work that needs to be done when control is passed from one method to another.


Video Answer


2 Answers

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.

like image 74
Tom Tresansky Avatar answered Oct 13 '22 14:10

Tom Tresansky


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!

like image 29
aioobe Avatar answered Oct 13 '22 13:10

aioobe