Static functions vs virtual functions in terms of speed - c++

I have some trouble. While planning my program, I consider 2 versions:
Use static methods in classes with additional parameter (pointer to copy of a class).
Use virtual methods (vtable)
What is faster? Why?
Edit: I need to make next algorithm: array should be storing pointers to method of different classes (they meet diff. game objects), for example, method Draw().
The main task is storing and calling methods of different classes.

At this point, you probably shouldn't be thinking about micro-optimisations at all - focus on choosing efficient algorithms, and making your code clear and correct; then identify any bottlenecks that prevent it from performing as required. Having said that, here are some thoughts in the unlikely event that you do find that virtual dispatch becomes an issue.
The two are not equivalent - the first (if I understand what you're saying) is trying to emulate a non-virtual function by explicitly passing a this-like pointer to a static function, and is likely to be exactly as fast as the non-static equivalent. It will behave differently to a virtual function, and so can't be used if you need virtual dispatch.
A non-virtual function will (almost certainly) be a bit faster than a virtual function - it's more likely to be inlined, and if not inlined it can be called directly rather than looked up at runtime. Therefore, only declare functions virtual when you need virtual dispatch.
In extreme circumstances, you might be able to save a level of indirection over virtual functions, by storing function pointers in the object rather than using the compiler-generated virtual dispatch. You should only do this as a last resort, if you find that virtual dispatch is a serious bottle-neck, and you can't sensibly redesign your algorithm to avoid it.

First, virtual functions and what you are proposing have different
semantics. If you need different behavior because you have different
types of objects, then it's highly unlikely that you can do better than
the compilers implementation of virtual functions. If you don't need
it, then just don't declare the function virtual.
And don't worry about the performance issue until you know it is one.
If, after getting the code to work, you do find performance issues due
to virtual function calls (usually because the compiler can't inline the
function, so you loose all of the optimizations which would follow
inlining), you can, punctually, avoid the virtual function cost if you
design the class correctly. Supposing the function in question is
f():
class Base
{
virtual void doF() = 0;
public:
void f() { doF(); }
};
class Derived : public Base
{
virtual void doF() { f(); }
public:
void f() { /* ... */ }
};
If you do this, and you have, for example, a tight loop where you're
constantly calling f() on the same object, you can do something like:
void
tightLoop( Base& object )
{
Derived& o = dynamic_cast<Derived&>( object );
for ( /* ... */ ) {
o.f();
}
}
If you do this, of course, tightLoop can only be called with an object
which is actually Derived.

Related

Does the virtual keyword affect performance of the base class methods?

I read that there is minor performance hit when calling virtual functions from derived classes if called repeatedly. One thing that I am not clear about is whether this affects function calls from the base class itself. If I call a method in the base class with the virtual keyword, does this performance hit still occur?
If I call a method in the base class with the virtual keyword, does this performance hit still occur?
That the virtual function is being called from the base class will not prevent virtual lookup.
Consider this trivial example:
class Base
{
public:
virtual get_int() { return 1; }
void print_int()
{
// Calling a virtual function from the base class
std::cout << get_int();
}
};
class Derived : public Base
{
public:
virtual get_int() { return 2; }
};
int main()
{
Base().print_int();
Derived().print_int();
}
Is print_int() guaranteed to print 1? It is not.
That the function print_int() exists in the base class does not prove that the object its called from is not derived.
Yes, there will be a performance overhead.
This is due to the fact that virtual functions in an inheritance hierarchy may or may not be overloaded by any derived class.
This requires a lookup in a v-table, as the base class doesn't know any better as to which class is dynamically implementing the function.
Edit: As mentioned, there may be some optimization, but this shouldn't be relied on
Virtual functions are implemented by a virtual function table. Each class has a table of the virtual functions' addresses. An instance of a class with a virtual function table has a pointer to the table, which is set by the constructor.
When the code calls a regular function, its address is hard coded into it. When it calls a virtual function it has to calculate *(*vtbl + 8 * function-offset), which requires two memory accesses. That's the overhead, which can be avoided in cases mentioned by others above.
Point is, if the same function is called repeatedly, much of the overhead might be avoided. The first call would bring the virtual function table from RAM to the CPU's cache, meaning it would be as cheap as 1-2 CPU cycles to fetch again. Doing an integer shift and an addition is rather cheap. If the compiler knows its the same function of the same class, it could calculate it once and reuse the value.
Since the question is "Is there a performance hit?" and not "Can there be a performance hit?", it is surprisingly tricky to answer accurately. This is because compilers are given a lot of leeway when it comes time to optimize your code, and they often make use of it. The process of eliminating that specific overhead has a particular name: devirtualization.
Because of this, whether a cost is incurred or not will depend on:
Which compiler is being used
The compiler version
The compiler settings
The linker settings
How the class is used
Whether there are any subclasses that override the method.
So what should you do with so much uncertainty? Since the overhead is minor in the first place, the first thing is to not worry about it unless you need to improve performance in that specific area. Writing structurally sound and maintainable code trumps premature optimisation every single time.
But if you have to worry about it, then a good rule of thumb is that if you are calling a non-final virtual function from a pointer or reference (which includes this) to a non-final class, then you should write code with the assumption that the tiny overhead associated with an indirect lookup through a vtable will be
paid.
That doesn't mean that the overhead will necessarily occur, just that there is a non-zero chance that it will be there.
So in your scenario, given:
class Base {
public:
virtual void foo() { std::cout << "foo is called\n"; }
void bar() { foo(); }
};
int main() {
Base b;
b.bar();
}
Base is not final
Base::foo is not final.
this is a pointer.
So you should operate under the assumption that the overhead might be present, regardless of whether or not it ends up being there in the final built application.

C++ Virtual function called normally or virtually?

Let's say a virtual function is called on a derived class object (on the object normally or through pointer/reference) that overrides that function but none of its derived classes override it. Is it called "virtually" through v-pointer or as a normal function so all optimizations / inlining apply to it? Does the standard say anything about this?
class CBase{
public:
virtual void doSomething() = 0;
};
class CDerived1 : public CBase{
public:
void doSomething() override { /* do stuff */};
};
class CDerived2 : public CDerived1{
};
//...
CDerived1 derived1;
CDerived1* p_derived1 = &derived1;
p_derived1->doSomething();
Does the standard say anything about this?
No. Whether the call uses the dynamic dispatch mechanism or not is not observable behavior. And the standard only concerns itself with observable behavior.
How much compilers "devirtualize" virtual calls is ultimately implementation defined. If you just have T t; and you do t.whatever(), then you shouldn't use a compiler that cannot devirtualize that.
Inlining affects devirtualization as well. Given the T t declarations, if you pass a function a reference to this object, and it takes a T& parameter, calls into it can be devirtualized if that function gets inlined.
But if its a non-inlined instance of that function (say, a pointer to the function, perhaps through std::function or whatever), then devirtualization is much harder. See, the compiler doesn't see the whole program, so it cannot see if you have some class somewhere that inherits from T and overrides that method.
Only the linker, through whole program optimization, could possibly see all class definitions that inherit from it. And even then... maybe not. Because it is still technically possible for DLLs/SOs to inherit from classes. And the non-inlined function ought to be able to take those T&s and call their overridden methods.
So once you leave a chain of inlining where the object's dynamic type and virtual calls into it are both visible to the compiler, devirtualization becomes far more difficult if not impossible.

Is a vtable generated when a virtual function is immediately marked final?

In this post: Does final imply override?, one of the answers showed that you can declare a function virtual AND final in the same declaration. An example was given that this prevents derived classes from mistakenly declaring a function with the same signature, so as to avoid confusion about which function is actually being called.
My question is, will a compiler still generate a virtual table for such a function? I would use this technique more often if I knew I wasn't incurring the vtable runtime overhead.
Yes!
Speaking practically...
Firstly, virtual tables are not generated for functions; virtual tables are generated for types (with each instance having a pointer to the relevant virtual table).
Eliding the entire virtual table for a type just because none of its function members can be further overridden would cause a wealth of problems; for example, the resulting binary still needs to be able to find that final type when the instance is referenced through a pointer-to-Base.
The only time this may make sense is with a type with virtual members, which are all final, and no base:
/**
* No members override (as there's no base)
* and no members may be overridden (as they're
* all `final`).
*/
struct Foo
{
virtual void foo() final {}
};
Does a compiler elide the virtual table then? Maybe. Probably not; why implement an alternative, special-case set of semantics for an extreme corner scenario?
Actually, it would probably break anyway in the general case when you later add new virtual members further up the hierarchy:
/**
* Well, this one needs a virtual table...
* ...and it's probably going to want to
* point out where Foo::foo() is...?
*/
struct Bar : Foo
{
virtual void bar() {}
};
Speaking legally...
Besides all this, I can't see any evidence that eliding the virtual table would be ABI-compliant, at least not under Itanium:
Each class that has virtual member functions or virtual bases has an associated set of virtual tables.
The final specifier is a C++ construct only with no ABI support, prohibiting such magic.
Speaking verifiably...
Ultimately, though, the only way to be sure is to actually examine the code that your compiler produces.

Is virtual method call thread-safe?

If several threads share a pointer to the same object, is it safe to call its virtual method? Of course we should assume that the method itself is thread-safe.
class Base {
public:
virtual int test(int arg) = 0;
};
class Derived1: public Base {
public:
virtual int test(int arg) { return arg * 2; }
};
class Derived2: public Base {
public:
virtual int test(int arg) { return arg * 3; }
};
//call in threads:
Base* base = (pointer to the same Derived1);
int value = base->test(1);
Yes, that's fine, provided the lifetime of the object *base exceeds that of the function call.
Calling virtual functions is normally implemented by looking up a vtable entry in the class. This vtable doesn't change, so for this implementation, it should be thread safe.
I don't think there is a general guarantee.
I've started this answer about three times, based on "Kerrek SB's comment on a previous answer. As I see it, without reading specs 'til my eyes bleed, I'm uncertain as to what shouldn't be threadsafe of a virtual call - the implementation of the "virtual function selection" (vtable or whatever it may be) should definitely not cause any problem, since it should just be a pointer to the function, which gets called based on the index [or similar] of the function chosen.
I'm sorry, this isn't an answer, but I have a very hard time seeing any scenario, on any processor that I know how it works (x86, 68K, 29k, ARM are ones that I've worked enough with to understand how a virtual function is implemented in assembler) where this would go wrong because of threads - assuming the other code is safe - if in the above example we were to run a second thread that modifies which element base points at, you could potentially have some sort of race-condition, where the value of base is pointing at the wrong set of virtual functions, or some such. But that's not the call itself, but the code modifying base that is 'not threadsafe'.
Of course, there may some "amateur" compiler around that solves virtual functions in some other way.
Of course, there wouldn't be a sensible workaround, should there be a problem - unless you block other threads for the entire duration of the virtual call - and if you have a class that implements threads by having a virtual function run(void *args) as the "this is what to run inside the thread", which is something I've seen a several times, that would pretty much kill that functionality completely!
So, basically, although I'm not able to refer to a spec section that says this is safe, I can't see any solution other than "it has to be".
If the method is threadsafe, then it is fine.
In short:
If not, C++ would not be useable at all for multithreaded programming.
In long:
After compiling the programm is constant. So its thread safe.
While loading (of modules) the runtime system changes data structures for
RTTI (dynamic_cast, ...). This is out of your scope but it should be implemented in a thread safe way.
After construction the type of your object does not change (you can with very dirty tricks). So all virtual functions of all your objects should not change. So its thread safe.
But you should consider that member of classes which can be seen as an replacment for virtual functions (boost function, loki functor, ...) may have value semantics and can change while called. In this case it should be documented or better interface for using them should be implemented.
In my opinion you can safely call a virtual method. Even in the case somebody use tries to mimic virtual functions you can expect normal (safe) behavior.

Why are destructors not virtual by default [C++]

Why doesn't C++ make destructors virtual by default for classes that have at least one other virtual function? In this case adding a virtual destructor costs me nothing, and not having one is (almost?) always a bug. Will C++0x address this?
You don't pay for what you don't need. If you never delete through base pointer, you may not want the overhead of the indirected destructor call.
Perhaps you were thinking that the mere existence of the vtable is the only overhead. But each individual function dispatch has to be considered, too, and if I want to make my destructor call dispatch directly, I should be allowed to do so.
It would be nice of your compiler to warn you if you do ever delete a base pointer and that class has virtual methods, I suppose.
Edit: Let me pull Simon's excellent comment in here: Check out this SO question on the code generated for destructors. As you can see, there's also code-bloat overhead to be considered.
Here's an example (not that I recommend writing such code):
struct base {
virtual void foo() const = 0;
virtual void bar () const = 0;
};
struct derived: base {
void foo() const {}
void bar() const {}
};
std::shared_ptr<base>
make_base()
{
return std::make_shared<derived>();
}
This is perfectly fine code that does not exhibit UB. This is possible because std::shared_ptr uses type-erasure; the final call to delete will delete a derived*, even if the last std::shared_ptr to trigger destruction is of type std::shared_ptr<void>.
Note that this behaviour of std::shared_ptr is not tailored to virtual destruction; it has a variety of other uses (e.g. std::shared_ptr<FILE> { std::fopen( ... ), std::fclose }). However since this technique already pays the cost of some indirection to work, some users may not be interested in having a virtual destructor for their base classes. That's what "pay only for what you need" means.
By the letter of the standard, a polymorphic class with a non-virtual destructor is not a bug. One specific action performed on such an object results in undefined behavior, but everything else is perfectly kosher. So given the otherwise lenient behavior of the standard in terms of what mistakes it allows programmers to make, why should the destructor be given special treatment?
And such a change would have costs, albeit mostly trivial ones: the virtual table will be one element larger, and the virtual dispatch associated with destructor calls.
To the best of my knowledge, no, there is no change in the behavior of destructors in this regard in C++11. I imagine it would say something in the section on special member functions, but it does not, and there is similarly nothing in the section of virtual functions in general.