Joe Pineda
Joe Pineda

Reputation: 5661

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?

Upvotes: 7

Views: 1053

Answers (4)

Handy999
Handy999

Reputation: 776

When it comes to the standard, the keyword inline has nothing to do with inlining.

The rules (in c++) are basically:

  • A function which is not declared inline can by only defined in one translation union. It still needs to be delared in each translation unit where it is used.
  • A function which is declared inline has to be defined in each translation unit where it is odr-used (ord-use means to call the function or to take the pointer,...).

So, in a standard project setting it is almost always correct to follow the following two rules. Functions that are defined in a header file, are always to be declared inline. Functions defined in a *.cpp-file are never declared inline.

This said, I think the compiler cannot really draw any conclusions about the programmer wanted inlining from using or not using keyword inline. The name of the keyword is an unfortunate legacy from a bad naming.

Upvotes: 0

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++.

Upvotes: 2

chux
chux

Reputation: 153447

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.

Upvotes: 1

François Andrieux
François Andrieux

Reputation: 29022

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.

Upvotes: 12

Related Questions