[C++] Detailed explanation of inline

Monday night, August 28, 2023


Table of contents


advantage

When a function is declared with inlinea keyword, the compiler inlines the body of the function into all calls to that function.

This improves execution efficiency because the overhead of making a function call is not required.

shortcoming

However, there are also downsides to overuse of inline functions:

  1. Executable file size increased. Executables get larger because function bodies are copied to each call site.
  2. Compile time increased. The compiler needs to process more code, so the compilation time increases.
  3. Optimization opportunities are reduced. Since the body of the function is scattered in multiple places, it is difficult for the compiler to optimize.

Conditions of Use

inlineKeywords should be used sparingly, and inlining is only wise for small functions.

In general, functions that satisfy the following conditions are suitable for inlining:

  1. The function body is small (usually no more than 10 lines).
  2. The function is called frequently (the calling overhead is relatively large).
  3. Functions don't call themselves recursively.
  4. The function body does not contain complex control flow structures such as loops and jumps.

Why calling a function has overhead

Function calls will generate certain overhead, mainly including:

  1. call command. Calling a function requires the use of the call instruction, such as callthe directive. This call instruction causes the program to jump to the body of the function and, on return, jump back to the calling point. This jump operation requires processor time and memory space, resulting in overhead.

  2. Parameter passing. Passing parameters to a function also requires time and space, and it is necessary to copy the actual parameter value to the formal parameter, or pass the address of the actual parameter to the formal parameter.

  3. The return address is stored. After a function call, the return address needs to be stored in order for the function to return. The storage of the return address also requires stack space, resulting in overhead.

  4. Register save. Before the function is called, the values ​​of the relevant registers need to be saved to the stack to prevent the function body from modifying them. Saving and restoring register values ​​also takes time and space.

  5. context switch. A function call needs to switch between the context of the current function and the context of the called function, and this switching operation also incurs overhead.

Although these overheads are relatively small for a single function call, if a function is called frequently, these overheads will accumulate and affect the execution efficiency of the program.

In short, the reason for the overhead of function calls is the time and space required for calling instructions, parameter passing, return address storage, register preservation, and context switching. Using inlinekeyword inline functions can reduce these overheads and improve efficiency.

for example

inline int add(int a, int b) { 
  return a + b; 
}

int main() {
  int sum = add(1, 2);
  // 这里,add()函数的函数体被内联,等价于:
  // int sum = 1 + 2; 
  // 函数调用开销被省略。
  return 0; 
}

Guess you like

Origin blog.csdn.net/m0_61629312/article/details/132550517