7 replies
by mtadeu
by sgringo
Virtual and inline ... at first blush, they do seem mutually exclusive. However, I believe you can combine them, due to the fact that there are occasions where virtual functions are resolved at compile time.
Let's say you construct an object of a class that inherits from some derivation hierarchy. You've made the base class destructor virtual, so as to ensure all destructors are called along the hierarchy when you destroy this object.
Normally, each destructor along the hierarchy is called via vtbl entries. *However*, let's say you made the base class destructor inline virtual. Now, all of the destructor calls (except for the first one) are resolved at compile time. Hence, you'll have saved vtbl lookup all throughout the destruction of a derived object. My understanding is, only by making the base class destructor virtual can you cause the destructor calls to be resolved statically. You might realize quite a performance gain with lots of heavily-nested objects popping in and out of existence constantly.
I hope that's clear ...
-Sheldon
by sgringo
I goofed on my wording. I said, "only by making the base class destructor virtual can you cause the destructor calls to be resolved statically"
What I meant to say:
"only by making the base class destructor INLINE can you cause the destructorcalls to be resolved statically"
Sorry for the confusion!
Thank for ur answer. Sorry but I didn't get ur point clearly. But i got the answer for my question. I would like to share the answer which i got from the net.
Thanks again
Question: Are "inline virtual" member functions ever actually "inlined"?
Answer:
Occasionally...
When the object is referenced via a pointer or a reference, a call to a virtual function cannot be inlined, since the call must be resolved dynamically. Reason: the compiler can't know which actual code to call until run-time (i.e., dynamically), since the code may be from a derived class that was created after the caller was compiled.
Therefore the only time an inline virtual call can be inlined is when the compiler knows the "exact class" of the object which is the target of the virtual function call. This can happen only when the compiler has an actual object rather than a pointer or reference to an object. I.e., either with a local object, a global/static object, or a fully contained object insidea composite.
Note that the difference between inlining and non-inlining is normally much more significant than the difference between a regular function call and a virtual function call. For example, the difference between a regular function call and a virtual function call is often just two extra memory references, but the difference between an inline function and a non-inline function can be as much as an order of magnitude (for zillions of calls to insignificant member functions, loss ofinlining virtual functions can result in 25X speed degradation! [Doug Lea, "Customization in C++," proc Usenix C++ 1990]).
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending