In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the final size of the program (i.e. the binary file size).
Ordinarily, when a function is invoked, control is transferred to its definition by a branch or call instruction. With inlining, control drops through directly to the code for the function, without a branch or call instruction. Inlining improves performance in several ways:
- It removes the cost of the function call and return instructions, as well as any other prologue and epilog code injected into every function by the compiler.
- Eliminating branches and keeping code that is executed close together in memory improves instruction cache performance by improving locality of reference.
- Once inlining has been performed, additional intraprocedural optimizations become possible on the inlined function body. For example, a constant passed as an argument, can often be propagated to all instances of the matching parameter, or part of the function may be "hoisted out" of a loop.
The primary cost of inlining is that it tends to increase code size, although it does not always do so. Inlining may also decrease performance in some cases - for instance, multiple copies of a function may increase code size enough that the code no longer fits in the cache, resulting in more cache misses.
Some languages (for example, C and C++) support the inline keyword in function definitions. This keyword serves as a "hint" to the compiler that it should try to inline the function. Compilers use a variety of mechanisms, including hints from programmers, to decide which function calls should be inlined.
Compilers usually implement statements with inlining. Loop conditions and loop bodies need lazy evaluation. This property is fulfilled when the code to compute loop conditions and loop bodies is inlined. Performance considerations are another reason to inline statements.
In the context of functional programming languages, inline expansion is usually followed by the beta-reduction transformation.
A programmer might inline a function manually through copy and paste programming, as a one-time operation on the source code. However, other methods of controlling inlining (see below) are preferable, because they do not precipitate bugs arising when the programmer overlooks a (possibly modified) duplicated version of the original function body, while fixing a bug in the inlined function.
Read more about Inline Expansion: Implementation, Benefits, Problems, Limitations, Selection Methods and Language Support
Famous quotes containing the word expansion:
“We are caught up Mr. Perry on a great wave whether we will or no, a great wave of expansion and progress. All these mechanical inventionstelephones, electricity, steel bridges, horseless vehiclesthey are all leading somewhere. Its up to us to be on the inside in the forefront of progress.”
—John Dos Passos (18961970)