[C++] Inline function ④ ( C++ compilation optimization - functions not modified by the inline keyword may also be inlined | C++ compiler inlining restrictions | several cases of inlining failure )





1. C++ Compiler Optimization - Functions not modified with the inline keyword may also be inlined




1. The uncertainty of function inlining



The current C++ compiler can compile and optimize,

  • The inline function declared with inline may not be allowed to be inlined by the compiler;
  • Ordinary functions that do not use inline declaration, if they are called frequently, the compiler may inline them in order to improve execution efficiency;

Uncertainty of inline functions: Compiler inline functions are determined based on compiler optimization strategies and code characteristics ;

  • There is no guarantee that all functions will be inlined;
  • Even if the function is inlined, there is no guarantee that the performance of the program will improve;

2. Inline optimization of C++ compiler


The inlining of simple and frequently called functions has a high probability of success, and the inlining of complex functions has a high probability of failure. Successful inlining may increase the size of the code, and may also slow down the running speed of the program;

You can adjust the parameters and optimization level of the C++ compiler to optimize the running effect of the compiled program;


3. Inline optimization details


Even if the function is not modified with the inline keyword, the C++ compiler decides whether to inline the function according to the function characteristics and calling frequency , combined with the current program execution efficiency and overall performance ;


The purpose of the inline function is to reduce the overhead of function calls and improve the execution efficiency of the program;

When the compiler decides whether to inline a function, it will consider factors such as the complexity, size and number of calls of the function;

If the function is relatively simple and is called frequently, the compiler may choose to inline it to improve the execution efficiency of the program;





2. C++ Compiler Inlining Restrictions




1. Several situations of inline failure


Several cases of inline failure: If the inline function has the following conditions, even if the inline keyword is used to declare the inline function, it is invalid;

  • There is a loop in the function: there cannot be any form of loop statement in the inline function , such as: for / foreach / while / do while loop;
  • There are many conditional judgments in the function: there cannot be too many conditional judgment statements in the inline function , and the conditional judgment means that there are invalid instructions, which is a waste of space; generally, there should be no more than 20 to 30 conditional judgment statements, and the specific number can be determined by compiler configuration;
  • The function body is huge: the code of the function body should not be too much, if there are too many function instructions, there will be a lot of instructions inserted at the calling position, which will waste a lot of code space;
  • Get the address of the function: When calling the function, try to get the address of the function. Since the inline function does not exist, it will be directly inserted into the calling position during compilation, and getting the address of the inline function will cause the program to fail to execute. Therefore, once you try to get the address of the function Inline function address, inline directly fails;
  • The declaration of the inline function is after the call: since the inline function cannot be declared, the declaration and definition of the inline function must be together. If the inline function is called before the declaration and definition, it means that the inline function has been declared separately . The inlining of the function will definitely fail, and it will be treated as a normal function;

2. Analysis of the essence of inline failure


If there are loop statements in the function / many conditional judgment statements/ huge function body/ operate on the address of the function/ declare the inline function separately, even if the inline inline function is written, the compiler will not agree to the inline request;


Compared with ordinary functions, the advantage of inline functions is that it saves the overhead of pushing/jumping/returning when the function is called;

If the function body is too large or the execution overhead of performing special operations is much greater than the overhead of pushing/jumping/returning, then the inline function will reduce the overall performance of the program, and the compiler will weigh the impact of the two on performance, agree / Veto the inlining request of the function;

Guess you like

Origin blog.csdn.net/han1202012/article/details/132644388