Pros and cons of 'inline'
First of all, I would like to state the facts I know about 'inline', so that you don't bother to restate them.
- An inline function is a special kind of function whose definition must be available in every translation unit in which the function is used.
- It is a hint to the compiler (which it is free to ignore) to omit the function call, and expand the body instead of the call.
- The only pro I know of is that (2.) may make the code faster.
- The only con I know if is that (1.) increases coupling which is bad.
Now let's consider templates. If I have a template library, I need to provide the definitions of the function templates in every translation unit, right? Let's forget about the controversial 'export' for a while, since it doesn't开发者_运维技巧 really solve the problem anyway. So, I come to a conclusion that there is no reason not to make a template function inline because the only con of inline I know of is there a priori.
Please correct me if I am wrong. Thanks in advance.
The only pro I know of is that (2.) may make the code faster.
May being the operative word. Inlined functions may make certain code paths faster, yes.
But an inlined function puts additional pressure on the instruction cache on most modern CPUs. If your function is too large to fit in the L1 instruction cache, it may actually run slower than performing a function call (for which the CPU can optimize by prefetching the function and its return site).
Inlining a function may also put undue pressure on the L2 cache - if an inlined function is used an unusually large number of times, the extra code size will increase the likelihood of cache misses, leading to long delays and pipeline stalls as the CPU twiddles its thumbs waiting for the memory bus to do something.
inline
is far from being a silver bullet. Compilers with aggressive optimization will ignore the inline
hint completely, as they will instead choose functions to inline based on heuristics such as code size or the presence of branches, irrespective of the presence or absence of the inline
keyword.
The only con I know if is that (1.) increases coupling which is bad.
This is something I've never heard. "Coupling" is a concept I've only heard used when describing the high-level relationships of code. It's more an issue of maintainability and generality of code. inline
is an issue of low-level code generation.
As to templates, again, an aggressively-optimizing compiler will inline if its heuristics show an advantage to doing so.
There is, however, a link-level issue to consider: You may need to declare a function or template inline
(or static
, depending on the situation) in order to eliminate duplicate symbols at link time or restrict symbol visibility. This is, of course, not an optimization.
In summary, don't bother using the inline
keyword except when specifically required.
I think you shouldn't bother with writing inline
or not before template function definitions. I think most compilers will inline small functions anyway, whether you ask them or not. Some compilers are even able to do this at link time for functions defined in only one translation unit.
Imho, the only point to the inline
keyword is to be able to define non-template functions inside headers, for those compilers who don't inline at link time (or when you want a "header only" library).
you don't need to provide the definition in every translation -- only the unes that use them ;)
it does matter whether you declare them inline or not. the compiler may acknowledge your use of the keyword (or of other force inline declarations). the compiler may generate exported classes/functions from your template definitions. if somebody asked me whether to inline by default or not (knowing they do not know the effect of using it), i would say: "don't use it", because the compiler has a better understanding of your program. optimizations are made differently among compilers.
example:
- let's say the compiler inlines some functions at your request
- inlining expansion during optimization typically caps out at a certain number of instructions... oops - the compiler just popped the stuff it should have inlined out of line because the body was too large with these templates which are inline by default.
- or you may end up with an extra-large binary
so you're tying one hand behind the compiler's back when you inline by default (provided, of course it acknowledges your use of the keyword).
in general: if you don't know, trust the compiler (writers).
there are a few cases where i declare inline by default (even if it just to reduce exported symbols).
other benefits of inlining:
- inlining can make code/functions smaller
- inlining can reduce the number of exported symbols
- it may also allow the compiler to make better optimizations, because it is able to 'see' more of the program during optimization.
By being inline, it removes the overhead of creating and destroying a new stack frame, and jumping to another location in the exe. This makes the code ever so slightly faster.
The down side is, ofcourse, the increase in exe size, because the function must be implemented where-ever it is called.
EDIT
Inlining functions is only worthwhile when it is called many times and the function size is small. Otherwise, the increase in file size may actually make the program slower, and it really isn't worth while.
精彩评论