Reading one book, I stumbled upon the built-in functions, I got the impression (thanks to the description of the author) that it is much faster than the usual (called function), but for some reason, when reading other people's sources, they are rarely found there, so what's the catch? Write in general terms when to make functions embedded, and when not. Thank you in advance.
2 answers
I will add the previous answer.
Firstly, inline is only a recommendation to the compiler to make the function embedded. The compiler can ignore it, if you do not agree that you need to substitute it. If it cannot be substituted, for example, when recursion or pointers to this function is used, it will not substitute it. Some compilers support __forceinline, which the compiler should not ignore if it can be inserted into the code.
Secondly, all inline functions must be defined in the header file, not in .cpp. By the way, even non-embedded functions that are in different cpp-files can be embedded by the linker if cross-module optimization is enabled.
I also assumed that the compiler can substitute only those parts of the code that can be executed. If it is known from the context of the function call that the conditions are always false, then the code of this branch will not be inserted. However, tests have shown that the compiler can do this without inline.
In general, I came to the conclusion that inline should be used in the most critical to the performance of places where every processor clock counts. At the same time, performance should be measured, since it is not a fact that it will give an increase.
PS:
for some reason, when reading other people's sources, they are rarely found there
Functions cannot read. This is a common mistake of using participle turns.
- onefor some reason, when reading other people's sources, they are found there quite rarely. I wrote very quickly, in the second window in Dota 2 was open :) PS you should not find fault with errors, because you understand the meaning that I wanted to convey :) :) - username76
- "Secondly, all inline functions should be defined in the header file, not in .cpp" - that is, if the function is declared embedded in .cpp, then the Inline specifier will be ignored by the compiler? - username76
- > Secondly, all inline functions should be defined in the header file, and not in .cpp. Probably this is a recommendation rather than a rule. Or am I wrong? - insolor
- > that is, if the function is declared embedded in .cpp, then the Inline specifier will be ignored by the compiler? Not. It will substitute this function only in those functions that are in the same compilation unit. This feature will not be available from other modules. When you try to call it, the linker writes that it is an unresolved external character. However, sometimes, when the compiler ignores inline, this error may not be. > This is probably a recommendation rather than a rule. Or am I wrong? That is the rule. The linker will generate an error (but not always). - gammaker
- In the case of gcc, if the function was declared as inline and without static, then a separate copy of this function is still created to be able to make an external call. - skegg
Apparently there are inline functions, if so, then they should be used without fanaticism. When the compiler encounters the inline , it stupidly copies a section of code to the place where the function call is made.
The advantages are:
- Saving time to call the stack - in a normal function, the parameters must be pushed and pushed out of the stack
- Absence of an overhead projector to call a function and return a value.
But there are also disadvantages.
- Increasing the size of the application - because pieces of code will be copied from one place to another
- Increase compile time (see above)
- Since the parameters of the call to the built-in function are emulated by assigning values to local variables, the number of variables can increase dramatically (and this is an increase in the same stack)
In general, the recipe is this: if a piece of code is small and few parameters and a piece is used 2-3 times, feel free to turn it in inline, and if not, do not suffer.
- Thanks for the clear and intelligible answer. - username76