ArticleslgStudy

science

Inline expansion

Inline expansion is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Inline expansion rather than just read about it. In short: In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compiling, without changing the source code (the text), while macro expansion occurs before compiling, and results in different text that is then processed by the compiler.

Key takeaways

  • Inline expansion belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Inline expansion to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Inline expansion from memory before moving on to harder problems.

Reference excerpt

In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compiling, without changing the source code (the text), while macro expansion occurs before compiling, and results in different text that is then processed by the compiler. Inlining is an important optimization, but has complex effects on performance. As a rule of thumb, some inlining will improve speed at very minor cost of space, but excess inlining will hurt speed, due to inlined code consuming too much of the instruction cache, and also cost significant space. A survey of the modest academic literature on inlining from the 1980s and 1990s is given in Peyton Jones & Marlow 1999.

Overview Inline expansion is similar to macro expansion as the compiler places a new copy of the function in each place it is called. Inlined functions run a little faster than the normal functions as function-calling-overheads are saved, however, there is a memory penalty. If a function is inlined 10 times, there will be 10 copies of the function inserted into the code. Hence inlining is best for small functions that are called often. In C++ the member functions of a class, if defined within the class definition, are inlined by default (no need to use the inline reserved word (keyword)); otherwise, the keyword is needed. The compiler may ignore the programmer’s attempt to inline a function, mainly if it is particularly large. Inline expansion is used to eliminate the time overhead (excess time) when a function is called. It is typically used for functions that execute frequently. It also has a space benefit for very small functions, and is an enabling transformation for other optimizations. Without inline functions, the compiler decides which functions to inline. The programmer has little or no control over which functions are inlined and which are not. Giving this degree of control to the programmer allows for the use of application-specific knowledge in choosing which functions to inline. 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. 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.

Effect on performance The direct effect of this optimization is to improve time performance (by eliminating call overhead), at the cost of worsening space usage (due to duplicating the function body). The code expansion due to duplicating the function body dominates, except for simple cases, and thus the direct effect of inline expansion is to improve time at the cost of space. However, the main benefit of inline expansion is to allow further optimizations and improved scheduling, due to increasing the size of the function body, as better optimization is possible on larger functions. The ultimate impact of inline expansion on speed is complex, due to multiple effects on performance of the memory system (mainly instruction cache), which dominates performance on modern processors: depending on the specific program and cache, inlining particular functions can increase or decrease performance. The impact of inlining varies by programming language and program, due to different degrees of abstraction. In lower-level imperative languages such as C and Fortran it is typically a 10–20% speed boost, with minor impact on code size, while in more abstract languages it can be significantly more important, due to the number of layers inlining removes, with an extreme example being Self, where one compiler saw improvement factors of 4 to 55 by inlining. The direct benefits of eliminating a function call are:

It eliminates instructions needed for a function call, both in the calling function and in the callee: placing arguments on a stack or in registers, the function call itself, the function prologue, then at return the function epilogue, the return statement, and then getting the return value back, and removing arguments from stacks and restoring registers (if needed). Due to not needing registers to pass arguments, it reduces register spilling. It eliminates having to pass references and then dereference them, when using call by reference (or call by address, or call by sharing). The main benefit of inlining, however, is the further optimizations it allows. Optimizations that cross function boundaries can be done without requiring interprocedural optimization (IPO): once inlining has been performed, added intraprocedural optimizations ("global optimizations") become possible on the enlarged function body. For example:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Inline expansion

Start with the simplest possible case. Write down what Inline expansion claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Inline expansion before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Inline expansion ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Inline expansion

In research
Inline expansion appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Inline expansion in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Inline expansion is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler optimizations, Programming language comparisons, Subroutines, so understanding it makes those chapters shorter.
In everyday life
Look for Inline expansion outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Inline expansion” →

How to study Inline expansion in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Inline expansion means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Inline expansion out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Inline expansion in simple terms?

In computing, inline expansion, or inlining, is a manual or compiler optimization that replaces a function call site with the body of the called function. Inline expansion is similar to macro expansion, but occurs during compiling, without changing the source code (the text), while macro expansion…

Why does Inline expansion matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Inline expansion?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Inline expansion.

Tags

  • Compiler optimizations
  • Programming language comparisons
  • Subroutines

Keep exploring