Function Inlining

The overhead associated with calling and returning from a function can be eliminated by expanding the body of the function inline, and additional opportunities for optimization may be exposed as well.

Example:

In the code fragment below, the function add() can be expanded inline at the call site in the function sub().

int add (int x, int y)
{
  return x + y;
}

int sub (int x, int y)
{
  return add (x, -y);
}
  

Expanding add() at the call site in sub() yields:

int sub (int x, int y)
{
  return x + -y;
}
  

which can be further optimized to:

int sub (int x, int y)
{
  return x - y;
}
  

Notes:

Function inlining usually increases code space, which is affected by the size of the inlined function, the number of call sites that are inlined, and the opportunities for additional optimizations after inlining.

Some compilers can only inline functions that have already been defined in the file; some compilers parse the entire file first, so that functions defined after a call site can also be inlined; still other compilers can inline functions that are defined in separate files.