Reputation: 1618
I was told long ago to make short functions/methods that are called often inline, by using the keyword inline and writing the body in the header file. This was to optimize the code so there would be no overhead for the actual function call.
How does it look with that today? Does modern compilers (Visual Studio 2010's in this case) inline such short functions automatically or is it still "necessary" to do so yourself?
Upvotes: 7
Views: 7137
Reputation: 51
Inlining may be done by a compiler in the following situations:
You marked the function as inline
and
The
inline
keyword tells the compiler that inline expansion is preferred.
The compiler treats the inline expansion options and keywords as suggestions.
You used the __forceinline
keyword (or __ attribute __((always_inline))
in gcc). This will make compiler to skip some checks and do the inlining for you.
The
__forceinline
keyword overrides the cost/benefit analysis and relies on the judgment of the programmer instead.
Microsoft compiler can also perform cross module inlining if you have turned on link time code generation by passing /GL
flag to the compiler or /LTCG
to the linker. It's quite clever in making such optimizations: try to examine the assembly code of modules compiled with /LTCG
.
Please note, that inlining will never happen if your function is:
Upvotes: 2
Reputation: 76523
inline
has always been a hint to the compiler, and these days compilers for the most part make their own decisions in this regard (see register
).
In order to expand a function inline, the compiler has to have seen the definition of that function. For functions that are defined and used in only one translation unit, that's no problem: put the definition somewhere before it's used, and the compiler will decide whether to inline the function.
For functions that are used in more than one translation unit, in order for the compiler to see the definition of the function, the definition has to go in a header file. When you do that, you need to mark the function inline
to tell the compiler and linker that it's okay that there's more than one definition of that function. (well, I suppose you could make the function static
, but then you could end up wasting space with multiple copies)
Upvotes: 11
Reputation: 158554
Enable warning C4710, this will warn you if a function which you define as inline is not inlined by the compiler.
Enable warning C4711, this will warn you if the compiler inlines a function not designated for inlining.
The combination of these two warnings will give you a better understanding of what the compiler is actually doing with your code and possibly whether it is worth designating inline functions manually or not.
Upvotes: 6
Reputation: 96311
Generally speaking, the inline
keyword is used more now to allow you to "violate" the one definition rule when you define a function in a header than to give the compiler a hint about inlining. Many compilers are getting really good at deciding when to inline functions or not, as long as the function body is visible at th4e point of call.
Of course if you define the function only in a source file non-inline, the compiler will be able to inline it in that one source file but not in any other translation unit.
Upvotes: 3
Reputation: 353
In short, yes, compilers will decide whether or not your function becomes inline
. You can check this question:
Does the compiler decide when to inline my functions (in C++)?
Upvotes: 1
Reputation: 2621
Aside from your main point (what amount of placing inline instructions in code is useful as of compilers today), keep in mind that inline functions are just a hint to the compiler, and are not necessarily being compiled as inline.
Upvotes: 1
Reputation: 5469
Yes, modern compilers will (depending on various configuration options) automatically choose to inline functions, even if they're in the source (not header) file. Using the inline directive can give a hint.
Upvotes: 1