Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a very short function become inlined even if it was not explicitly defined as inline?

I know in advance that, when writing a program in C or C++, even if I declare a function as "inline" the compiler is free to ignore this and decide not to expand it at each (or any) call.

Is the opposite true as well? That is, can a compiler automatically inline a very short function that wasn't defined as inline if the compiler believes doing so will lead to a performance gain?

Two other subquestions: is this behaviour defined somewhere in the ANSI standards? Is C different from C++ in this regard, or do they behave the same?

like image 763
Joe Pineda Avatar asked Nov 27 '18 15:11

Joe Pineda


People also ask

When can a function be inlined?

In other words, one may say that such a function is expanded in-line, hence named as an inline function. Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program.

How do you know if a function is inlined?

If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.

When might a compiler choose not to inline a function declared as inline?

The compiler can't inline a function if: The function or its caller is compiled with /Ob0 (the default option for debug builds). The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other). The function has a variable argument list.

When can an inline function not be used?

When we should avoid the use of inline? We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.


3 Answers

inline is non-binding with regards to whether or not a function will be inlined by the compiler. This was originally what it was intended to do. But since then, it's been realized that whether or not a function is worth inlining depends as much on the call site as the function itself and is best left to the compiler to decide.

From https://en.cppreference.com/w/cpp/language/inline :

Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those optimization choices do not change the rules regarding multiple definitions and shared statics listed above.

Edit : Since you asked for C as well, from https://en.cppreference.com/w/c/language/inline :

The intent of the inline specifier is to serve as a hint for the compiler to perform optimizations, such as function inlining, which require the definition of a function to be visible at the call site. The compilers can (and usually do) ignore presence or absence of the inline specifier for the purpose of optimization.

like image 162
François Andrieux Avatar answered Oct 19 '22 12:10

François Andrieux


Regarding the relation between C and C++, the inline specifier is treated differently in each language.

  • In C++: inline functions (and function like entities, and variables (since C++17) ) that have not been previously declared with internal linkage will have external linkage and be visible from other compilation units. Since inline functions (usually) reside in header files, this means that the same function will have repeated definitions across different compilation units (this is would be a violation of the One definition rule but the inline makes it legal). At the end of the build process (when linking an executable or a shared lib), inline definitions of the same entity are merged together. Informally, C++ inline means: "there may be multiple identical definitions of some function across multiple source files, but I want them to end up as a unique definition".
  • In C: If extern is not explicitly specified, then an inline function definition is not visible from other translation units, different translation units may have different definitions with inline specifier for the same function name. Also, there may exist (at most) one definition for a function name that is both inline and extern and this qualifies that function as the one that is externally visible (ie gets selected when one applies the address of & operator to the function name). The One definition rule from C and its relation with extern and inline is somehow different from C++.
like image 25
Julien Villemure-Fréchette Avatar answered Oct 19 '22 12:10

Julien Villemure-Fréchette


can a compiler automatically inline a very short function that wasn't defined as inline if the compiler believes doing so will lead to a performance gain?

Limitation:
When code uses a pointer to the function, then the function needs to exist non-inlined.

Limitation:
When the function is visible outside the local .c file (not static), this prevents simplistic inlined code.

Not a limitation:
The length of the function is not an absolute limitation, albeit a practical one.

I've worked with embedded processor that commonly inline static functions. (Given code does not use a pointer to them.)

The usefulness of the inline keyword does not affect the ability for a compiler to inline function.

like image 24
chux - Reinstate Monica Avatar answered Oct 19 '22 14:10

chux - Reinstate Monica