Reputation: 39
I have a set of arithmetic-only functions, whose calls are not determined at the compilation but at run time. I intended to create a array of pointers to all of them, and to handle the call of them through the array indices (e.g. if (a>3) call the 3rd one).
Those functions will be called heavily and repeatedly in a loop, so they must be inlined for performance.
My question is, will such a call through inline member function pointers end up being inlined?
Thanks!
class foo{
private:
int f(int x){return x;}
int (foo::*pf)(int);
public:
foo(){
pf=&foo::f;
(*this.*pf)(3); //will this call be inlined?
f(3); //this call is surely inlined
}
};
int main(){
foo f;
return 0;
}
Upvotes: 0
Views: 871
Reputation: 129374
If the code decides at runtime which function to call, clearly, the function CAN NOT be inlined - there is no choice but to call it through a pointer. It is really a case of "can the compiler figure out what is happening or not". In a case where you call a function through a pointer based on some conditions, then the compiler will need to understand how the conditions affect which pointer is used. If the compiler can't decide this upon the point of compiling the code, it MUST use a function pointer.
So in your example code, the compiler can (if it chooses to do so) inline both foo
and figure f
.
But in case of, say,
In a constructor, for example:
if (x > y) pf = foo::f(); else pf = foo::g();
In some other code, where the compiler doesn't have direct knowledge of what the x & y values were in construction:
*blah.*pf();
the call can not be inlined, because the compiler doesn't know if f
or g
is the function to call.
I hope this makes sense.
[I also wonder why you can't use virtual functions...]
Upvotes: 1
Reputation: 12797
" Those functions will be called heavily and repeatedly in a loop, so they must be inlined for performance."
there is no such thing in now days.
for your inline ans : that call will be inlined or not depend on compiler.
Upvotes: 0
Reputation: 10613
First of all, there's never a guarantee that a particular call will be inlined (short of using compiler-specific extensions) so the "surely" in your code is not a sure bet.
The function-pointer call in your example could be inlined (since the compiler can tell what function is being called using static analysis) but that example is contrived and I doubt your code is that simple.
With all that said, the more important question is why are you worrying about performance right now? Do you have actual hotspots identified using a profiler that you are trying to improve, or do you just have a "feeling" that this will be an issue?
Focus on writing clean, understandable and maintanable code, and then after it's written and debugged, you can focus on performance-tweaking it with micro-optimizations.
Upvotes: 3
Reputation: 208353
In the general case, no, it won't. In your particular case where the compiler sees how the pointer to member is obtained and everything is visible to the compiler, it can.
Upvotes: 0