views:

60

answers:

3

Possible Duplicate:
Benefits of inline functions in C++?

Hi, what is the exact usage of inline functions, what exactly inline function does in brief. on what basis a programmer has to choose the function as inline. i Googled the answer but still i prefer stackoverflow.

+1  A: 

Function inlining is a performance optimization done by a compiler

For a typical function compiled to machine code, you will have function prologs and Fepilogs. The prolog and epilog setup the stack frame, save registers, allocate space for locals, and do whatever else is needed per the calling convention. Doing all of this takes CPU cycles. For really small functions (e.g. property getters and setters), this cost can be high relative to the actual work done in the function. This is where inlining comes to save the day :)

An inline function is a function that you've defined that the compiler has decided to include directly into methods that call it instead of generating call instructions. In other words, the actual machine code of the target inline function is "inlined" into the body of the calling method. For example, if Foo called Bar and Bar was inlined, and you ran your program under a debugger, you would see the instructions for Bar in the body of Foo. The epilog and prolog for Bar can be discarded.

Most of the time, you should just let the compiler determine when to inline the function for you. Inlining is not magical or free. It increases code size and live ranges of variables, and it forces the compiler to juggle more data with fewer locations. Because of this, the compiler has a complicated set of heuristics to determine when the cost is worth it. Most compilers do allow for inlining hints. For Visual Studio, see inline, __inline, and __forceinline. Even with these hints, however, the compiler is free to ignore you and do what it wants.

Related compiler concepts are outlining and partial inlining (see here) that attempt to move cold, infrequently hit blocks of code out of the body of a large method in order to improve cache behavior and even pave the way for inlining the outlined method.

Chris Schmich
Inlining doesn't necessarily inflate code. When a call is inlined the compiler can skip the code for organizing the call and so inlining can actually deflate code.
sharptooth
+1  A: 

Definition by C++ FAQ Lite:

When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro).

This can, depending on a zillion other things, improve performance, because the optimizer can procedurally integrate the called code — optimize the called code into the caller.

See the same page for examples and more details.

Igor Oks
A: 

http://www.parashift.com/c++-faq-lite/inline-functions.html

Even if you specify the inline flag, the compiler may ignore it. If you are writing a library, and have really thought hard about the performance guarantees you want to make (and profiled the heck out of it), then you might consider adding inline to your functions or methods. Even then, there is no guarantee you will see any difference in the compiled code, because the compiler may have already decided to inline them, or ignore your request.

A warning about performance: inlining won't always increase your performance, because it also increases the size of your code.

Profile your app, find your bottlenecks, consider changes at an algorithmic level, and only when you need to squeeze the last bit of performance out in one of your inner loops would you bother trying to inline functions, unroll loops, or other micro optimizations.

Merlyn Morgan-Graham