Quote:Original post by mattnewportQuote:Original post by Dave
TBH it's barely worth inlining anything at all. Any language directive is only a hint and without it the compiler will still do what it thinks is best.
Some compilers, like Visual C++, can sometimes inline functions that are not declared as inline through whole program optimization and link time code generation. In general though if you have a function that is a good candidate for inlining (generally a short and simple function that you know is called with high frequency in performance sensitive code) then it is worth declaring it as inline and defining it in a header where it is visible to all callers as the optimizer is more likely to be able to inline it where appropriate.
This is true. Typically, the inliner in a compiler is implemented as a set of heuristics. The compiler weighs the characteristcs of a give inline candidate. Typically it might look at the size of the callee, whether the user said to inline it, does it have EH, etc. If you specify __forceinline you will tell the compiler to ignore the heuristics and always inline. However, there are still cases when this is NOT going to inline the function. This can happen if the compiler does not know how to inline the function. Sometimes it's just too difficult. For instance, inlining SEH into C++ EH (which introduces a nasty thing called 2-pass flow), inlining vararg functions, or inlining function with inline asm.
However, because __forceinline is often used for correctness (ex: wrapping an intrinsic call that must be inlined for it to work correctly), there are no instances where a __forceinline will be ignored because it provides negative benefit.
But, in the end, the moral of the story is probably to let the compiler do what it wants. It's very good at determining what the best inlining pattern is.
(BTW, I work on the VC++ backend)