In programming, inlining is the process of replacing a function call within the caller with the code of the callee itself. This is done via compiler directives, and can open up a lot of optimisation opportunities.

With inlining, the compiler can detect dead branches that are never executed, repeated computation of an expression when the value is already known, or that the function may only return a certain type of value. We can also use inline functions to avoid using function macros. Easy enough!

Compilers can also automatically inline some code.

Manually

In C/C++, we can define functions as inline with the inline keyword. The compiler only looks at the definition, not the declaration to determine whether or not to inline a function.

  • All pointers to the same inline function are equal.
  • A function definition can be used in several source files without causing a multiple definition error.
  • An inline function that is not used in a source file will be completely absent from the binary of that file (reducing its size).

In general, it’s a good idea to set small functions as inline. This is because function calls have some overhead (because space on the stack needs to be allocated and local variables need to be saved). Inlining avoids this overhead while not significantly increasing the size of an executable’s binary.