Is there any way to inline just some selective calls to a particular function not all of them? cause only form I know is declaring function as such at beginning and that's supposed to effect all calls to that function.
views:
115answers:
6There's no gaurantee that the compiler will inline calls to functions. It is just an suggestion not an command.
please note that if you use the inline keyword, this is only a hint for the compiler. You cannot really control what the compiler inlines and what not.
If your compiler would follow your hints you could do:
inline void doItInline() {...}
void doItNonInline() {doItInline();}
Now you can call the method either inline'd or not.
You can try to force it the other way around.
even though if you ask the compiler to inline, it can't do so if the definition is not available in the current compilation unit and has to create a call.
if you make sure the definition IS available, the compiler MIGHT inline it.
wrap the in-line version with a non-inline defined version?
inline void f_inline () {}
void f_not_inline () {f_inline ();}
A solution would be to use a macro :
#define myfunc_inlined(a,b) (a + b)
int myfunc(int a, int b)
{
return myfunc_inlined(a,b);
}
int main()
{
int i = myfunc_inlined(1, 3); #macro call
int j = myfunc(1, 3); #standard call
}
First, some background on inlining.
There are multiple phases where inlining might occur (for example):
- at compile time, the compiler may decide (or not) to inline the call.
- at link time, if LTO are enabled, the linker might decide to inline some calls.
- at load time, for the languages that support it.
All of them share a common point: they inline selectively, ie if it is worth at the point of call.
In C++, you can hint to the compiler that a function might be worth inlining using the inline
, but it'll still perform a performance analysis with its own heuristics. The main use being that free functions declared inline do not lead to link failures because of duplicate symbols.
MSVC also supports __forceinline
which forces inline... unless it's impossible (virtual call, function pointer, /O0, ...)
Therefore: it's not really possible to force / prevent inlining as far as I know. LTO even allows (in some circumstances) the inlining of virtual methods (which requires whole program analysis, thus is only suitable at the exec stage).