views:

115

answers:

6

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.

A: 

There's no gaurantee that the compiler will inline calls to functions. It is just an suggestion not an command.

Als
Yep, beat me to it.
Nik Reiman
+5  A: 

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.

Philipp
Nothing prevents the compiler from inlining calls to `doItNonInline` anyway!
Konrad Rudolph
Perhaps the bigger question here is: are there good reasons for wanting to force a function not to be inlined by the compiler?
MadKeithV
@Philipp_Now regarding this solution, what about doing it with class operators like equal operator cause they're some kinda function too?
Pooria
Please note that I wrote "If your compiler would follow your hints", I didn't claim this was possible with a compiler I know. (@Konrad: For MS VC++ you can tell the compiler not to inline anything that is not marked inline, but that doesn't imply it would inline everything that is marked inline.)
Philipp
The `inline` is *not* only a hint for the compiler. It introduces changes to the rules for the number and locations of allowed definitions for the function. The compiler is not allowed to ignore these rule changes even if it doesn't perform inline expansion of calls to an `inline` function.
Charles Bailey
+1  A: 

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.

Markus Winand
Oh, you're not using msvc, are you?
avakar
Even if the compiler can't inline a call, LTO might inline it anyway
TokenMacGuy
mostly gcc :) the question does not mention which system :/
Markus Winand
@Markus Winand, my point was that msvc can inline calls across translation unit boundaries. :) Also, there is an experimental branch of gcc that can do that (through link time code generation).
avakar
+1 for a raising a highly relevant and confusing point, even though I think in light of TokenMacGuy's insight you might change "can force" to "can try to force"...
Tony
@avakar - got that. Still that approach could possibly work in the environment where needed?
Markus Winand
@Markus Winand, it could possibly work in the environment where needed *for now*. It certainly isn't a solid piece of engineering.
avakar
@tony - done that.
Markus Winand
A: 

wrap the in-line version with a non-inline defined version?

inline void f_inline () {}

void f_not_inline () {f_inline ();}

John
Nothing prevents the compiler from inlining calls to `f_not_inline` anyway!
Konrad Rudolph
Or not inlining calls to `f_inline` :)
aib
@John_what about when dealing with user-defined operators since they're functions too?
Pooria
A: 

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
}
OMG_peanuts
A macro is certainly not a function, too many issues to be worth mentioning for the job.
Matthieu M.
+2  A: 

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

Matthieu M.
`__forceinline` is a lot more that just a hint, it overrides the compilers choice, unless the function cannot be inline'd, see here: http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx
Necrolis
@Necrolis: bad wording on my part, sorry, I consider it a hint because the compiler may not end-up inlining it. I'll clarify.
Matthieu M.