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.