Inline expansion

Inline expansion or inlining for short is a compiler optimization which "expands" a function call site into the actual implementation of the function which is called, rather than each call transferring control to a common piece of code. This reduces overhead associated with the function call, which is especially important for small and frequently called functions, and it helps call-site-specific compiler optimizations, especially constant propagation. The main drawback is that the expansion usually results in a larger binary code, which can actually hurt performance if it damages locality of reference or exceeds resource constraints.

In the context of functional programming languages, inline expansion is often referred to as beta reduction, a term used in the lambda calculus, the formal language underlying these languages.

Contents

Implementation

Once the compiler has decided to inline a particular function, it is usually a simple matter to do so. Depending on whether one wants cross-language inline functions, the inlining can be done with either a high-level intermediate representation, like abstract syntax trees, or a low-level intermediate representation. In either case, one simply computes the arguments, stores them in variables corresponding to the function's arguments, and then inserts the body of the function at the call site.

Function inlining can also be performed at link-time, which enables inlining of functions whose source is not available such as library functions (see link-time optimization) and at run time, which enables using dynamic profiling information to make better decisions about which functions to inline, as in the Java Hotspot compiler.

Here's a simple example of inline expansion performed "by hand" at the source level in the C programming language:

 int pred(int x) {
      if (x == 0) return 0; else return x - 1;
  }
  
 

Before inlining:

 int f(int y) {
      return pred(y) + pred(0) + pred(y+1);
  }
 

After inlining:

 int f(int y) {
      int temp = 0;
      if (y   == 0) temp += 0; else temp += y       - 1;
      if (0   == 0) temp += 0; else temp += 0       - 1;
      if (y+1 == 0) temp += 0; else temp += (y + 1) - 1;
      return temp;
  }
 

Note that this is only an example; in an actual C application, it would be preferable to use an inlining language feature such as parameterized macros or inline functions to tell the compiler to perform this transformation.

Benefits

Inline expansion itself is an optimization, since it eliminates call overhead, but it is much more important as an enabling transformation. That is, once the body of the function is expanded in the context of its call site, often with arguments that may be fixed constants, the code is opened to a variety of new optimizations that were not possible before. For example, a branch using an argument may turn out to be always true or always false in this one case, allowing dead code elimination, loop-invariant statements may be moved outside a loop, or a variable may become a candidate for induction variable elimination.

In our C example, we see that optimization opportunities abound. We can reduce it in the following steps:

Our new function looks like:

 int f(int y) {
      if (y == 0)
          return y;            /* or return 0 */
      else if (y == -1)
          return y - 1;        /* or return -2 */
      else
          return y + y - 1;
  }
 

Problems

Replacing a call site with an expanded function body can present several problems that may make this "optimization" actually hurt performance:

Typically, a compiler is aware of these issues and strives to choose which functions to inline in such a way that performance is only enhanced in most cases.

Selection methods and language support

Many compilers aggressively inline functions wherever it is beneficial to do so. Although this can lead to larger executables, this has nevertheless become more and more desirable as growth of memory capacities have outpaced growth of CPU speed. This automatic type of inlining is a critical optimization in functional languages and object-oriented programming languages, which rely on it to give enough context to their typically small functions to make classical optimization effective.

In imperative programming languages, the approach to inline functions is quite different, since functions are typically much larger. Usually only obvious or key functions are inlined, using language features like inline functions, or in their absence, simple source-level constructs such as parameterized macros. In either case, the programmer chooses which functions to inline manually, although the compiler may in some cases not be able or willing to inline a function marked for inlining.

See also

External links

See also: Inline expansion, Abstract syntax tree, Argument, Beta reduction, CPU cache, C programming language, Compiler, Compiler optimization, Constant, Constant propagation