Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile time elimination of virtual tables?

Tags:

c++

oop

c++11

Assuming i have this hierarchy :

class Super
{
 public:
    virtual void bar();
};

class Sub : public Super
{
 public:
    virtual void bar() override;
};

is there a way for me to avoid vtables despite using the virtual key word? (curiosity) i have read something about a compiler optimization that eliminates vtables when the object is known during compile time, I'm not really sure, been digging around google for a while, but could't find any answers, so does it mean these?

Sub sb;
sb.bar(); //avoids vtable?
Super& sr = sb;
sr.bar(); //avoids vtable?
Super* srp = &sb;
srp->bar(); //avoids vtable? 
like image 214
Epsilon_ Avatar asked Apr 19 '16 08:04

Epsilon_


People also ask

Is vtable created at compile time?

Virtual tables are created during compilation but used at runtime. Show activity on this post. As you say, the virtual function table (and other polymorphic information) for each class is generated at compile time. Each object contains a pointer to the correct table for its dynamic type.

What happens when virtual table is created?

The vtable is created at compile time. When a new object is created during run time, the hidden vtable pointer is set to point to the vtable. Keep in mind, though, that you can't make reliable use if the virtual functions until the object is fully constructed.

Are virtual methods slower?

Virtual functions are by definition slower than their non-virtual counterparts; we wanted to measure the performance gains from inlining; using virtual functions would make the difference even more pronounced.

How slow are virtual functions Really?

Virtual functions are slow when you have a cache miss looking them up. As we'll see through benchmarks, they can be very slow. They can also be very fast when used carefully — to the point where it's impossible to measure the overhead.


2 Answers

One of the gcc developers has a whole series of blog posts about devirtualization. I think he is also active on SO, so there may be a chance that he responses.

However, devirtualization deals mostly with eliminating virtual dispatch by analyzing the program flow and possible types. I don't think it removes the virtual table in general, but there is an example in the second article where a virtual gets inlined and can then be evaluated completely at compile time through constant propagation. In that case, the compiler/linker transformed the program to not use the class at all, and thus it should not contain any object or vtable.

like image 157
Jens Avatar answered Sep 22 '22 14:09

Jens


Sub sb;
sb.bar(); //avoids vtable?

The above will never need to use a vtable for dispatch, as the runtime type is known (i.e. it's known to be the same as the compile-time/static type, namely Sub).

Super& sr = sb;
sr.bar(); //avoids vtable?


Super* srp = &sb;
srp->bar(); //avoids vtable? 

In these cases, if the pointer/reference and usage appear in the same function, the optimiser may well be smart enough to avoid dispatch via a vtable. If the pointer or reference is passed to some other out-of-line function that migth be called with other types of pointers, then vtable-based dispatch will normally be needed.

More generally, the C++ Standard doesn't make any stipulations about how runtime polymorphism is implemented, so there is no guaranteed, portable way to eliminate "vtables".

That said, your best bets to minimise use of the vtable for dispatch are:

  • to mark overrides final when the freedom to override further is not actually required, and

  • to keep the implementation inline (even if implicitly - by having the function implementation in the class definition)

To see if either/both help, you'll have to experiment with or read the docs for your own compiler / tool-chain, optimisation flags etc..

An unused vtable may or may not be removed by the linker: you may want to experiment with cross-object linker optimisation flags if you have multiple translation units.

like image 40
Tony Delroy Avatar answered Sep 26 '22 14:09

Tony Delroy