Function Inlining

Last Updated : 12 Mar, 2026

Prerequisite: Phases of Compiler

The fifth phase of a compiler is code optimization. This phase applies various optimization techniques to the intermediate code to improve the performance of the generated machine code. Function inlining is used to improve the performance of the code. The function call is replaced by the code of the function being called. Function inlining should be applied to small functions only. With the help of function inlining, we can reduce the execution time. It reduces the function call overhead and saves the time of pushing the function into and out of the stack.

32.png
Before function inlining



32.png
After function inlining


Function inlining helps in enabling more optimizations. With the help of inlined code, code optimizations like copy propagation and constant folding can be applied easily. Thus reducing the execution time.

C++
int multiply(int a,int b)  //called function
{
  return a*b;
}
int func(int x,int y)      //calling function
{
  return multiply(x,y);   //function call
}

//after function inlining
int func(int x,int y)
{
  return x*y;           //function call replaced with called function
}

In the above example, we are performing multiplication in function func() by passing the variables to multiply(). A function call introduces overhead such as parameter passing, stack frame creation, and return operations. Inlining removes this overhead, which can improve performance when small functions are called frequently.

To simplify this, we will apply function inlining and will replace the function call with the called function's body. This helps in saving the extra computation time and performs the same task of multipling the same numbers.

Advantages

  • Reduces function call overhead
  • Improves execution speed for small functions
  • Enables other optimizations (constant folding, propagation)
  • Improves instruction locality

Disadvantages

  • Increased code size
  • Increased compilation time
  • Reduced maintainability
Comment
Article Tags:

Explore