Inline Function
Let's look at the following program
When main() calls sum(), it needs to push all parameters into stack frame, create a new stack frame for sum(), and return the stack frame back to system after it quits. But what if we call sum() thousands of times? Notice that sum() function only involves three operations (mov, add, and mov), then it is a huge amount of overhead in calling the function comparing to the calculation itself. Under this circumstances, we had better mark function sum() as inline.
An function with inline simply does one work: it expand all the commands inside the function, and insert them into where the function is called, during the compiling process. In this way, the function calling process is eliminated.
Since there's no function call, there's no need to generate function symbols inside the symbol table. To verify this, we can compile this code with O2 compiler optimization as g++ -c main.cpp -O2
and dump the object file with objdump -t main.o
.
However, not every function marked with inline
will be converted into inline function. A recursive function, for example, which calls itself constantly, can not be inlined because the times it is called is only known at runtime. Moreover, if the function has too many lines, it may also not be inlined.
Therefore, the keyword inline
is just a suggestion for the compiler to make it inlined. It is up to the compiler to decide whether make it inlined or not.
In VS Code, inline
only works in the release version. The debug version makes it unavailable in order to facilitate the debugging of the program.
Last updated