27

Does using virtual inheritance in C++ have a runtime penalty in compiled code, when we call a regular function member from its base class? Sample code:

class A {
    public:
        void foo(void) {}
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};

// ...

D bar;
bar.foo ();
Regexident
  • 29,336
  • 10
  • 93
  • 99
Goofy
  • 4,877
  • 5
  • 38
  • 56
  • That's not an interesting example. As the cost of bar.foo() is fixed (because the method is not virtual) and does not change with inheritance virtual or otherwise. – Martin York Apr 05 '11 at 16:19
  • When you compare code generation between virtual and non-virtual functions|(base classes) , make sure the compiler has **no idea what the dynamic type is**! – curiousguy Aug 05 '12 at 01:18
  • "_As the cost of bar.foo() is fixed (because the method is not virtual)_" that is not correct. The dynamic type is known here, so virtualness is irrelevant. `foo` could as well be virtual. – curiousguy Aug 05 '12 at 04:23

7 Answers7

21

There may be, yes, if you call the member function via a pointer or reference and the compiler can't determine with absolute certainty what type of object that pointer or reference points or refers to. For example, consider:

void f(B* p) { p->foo(); }

void g()
{
    D bar;
    f(&bar);
}

Assuming the call to f is not inlined, the compiler needs to generate code to find the location of the A virtual base class subobject in order to call foo. Usually this lookup involves checking the vptr/vtable.

If the compiler knows the type of the object on which you are calling the function, though (as is the case in your example), there should be no overhead because the function call can be dispatched statically (at compile time). In your example, the dynamic type of bar is known to be D (it can't be anything else), so the offset of the virtual base class subobject A can be computed at compile time.

James McNellis
  • 338,529
  • 73
  • 897
  • 968
  • 3
    @James: I think you're confusing with virtual inheritance with virtual functions, aren't you? – Nawaz Apr 05 '11 at 15:00
  • 2
    @Nawaz: Nope. Given a `B*` to an arbitrary `B` object that might be the subobject of some derived class, what do you know about where the `A` base class subobject is relative to that `B*`? At compile time, you don't know where the `A` base class subobject is. – James McNellis Apr 05 '11 at 15:06
  • @James: So how does the compile decide? – Nawaz Apr 05 '11 at 15:08
  • @Nawaz: The compiler doesn't decide anything: it generates code to thunk through a vptr or a lookup table or to use some other implementation-specific method to locate the base class subobject. – James McNellis Apr 05 '11 at 15:09
  • 2
    @Martin: My understanding is that you just need something along the lines of `A* a_ptr = b_ptr + b_ptr->vtable[index_of_offset_of_virtual_base_A]` (conceptually). For a given derived class (like `D`), the `A` subobject is located at a known offset from the `B` subobject. However, if you have a `B*` pointing to some arbitrary object, you don't know _relative to that pointer_ where the `A` subobject is without checking the vtable because the most derived type might be `B` or `D` or `[something else]`. (I may not be expressing myself clearly here; I've only just gotten coffee this morning.) – James McNellis Apr 05 '11 at 16:20
  • I hope compilers are clever enough to optimize this away when the subobject is final – Jean-Bernard Jansen Aug 30 '16 at 16:48
  • @Jean-BernardJansen Results from my 2020 quick testing: [GCC is](https://godbolt.org/z/6NRMWR), but only starting with version 10 (need at least -O2), clang has been for a long time, and MSVC still isn't. – Arne Vogel Jun 25 '20 at 20:22
15

Yes, virtual inheritance has a run-time performance overhead. This is because the compiler, for any pointer/reference to object, cannot find it's sub-objects at compile-time. In constrast, for single inheritance, each sub-object is located at a static offset of the original object. Consider:

class A { ... };
class B : public A { ... }

The memory layout of B looks a little like this:

| B's stuff | A's stuff |

In this case, the compiler knows where A is. However, now consider the case of MVI.

class A { ... };
class B : public virtual A { ... };
class C : public virtual A { ... };
class D : public C, public B { ... };

B's memory layout:

| B's stuff | A's stuff |

C's memory layout:

| C's stuff | A's stuff |

But wait! When D is instantiated, it doesn't look like that.

| D's stuff | B's stuff | C's stuff | A's stuff |

Now, if you have a B*, if it really points to a B, then A is right next to the B- but if it points to a D, then in order to obtain A* you really need to skip over the C sub-object, and since any given B* could point to a B or a D dynamically at run-time, then you will need to alter the pointer dynamically. This, at the minimum, means that you will have to produce code to find that value by some means, as opposed to having the value baked-in at compile-time, which is what occurs for single inheritance.

Puppy
  • 141,834
  • 35
  • 244
  • 454
8

At least in a typical implementation, virtual inheritance carries a (small!) penalty for (at least some) access to data members. In particular, you normally end up with an extra level of indirection to access the data members of the object from which you've derived virtually. This comes about because (at least in the normal case) two or more separate derived classes have not just the same base class, but the same base class object. To accomplish this, both of the derived classes have pointers to the same offset into the most derived object, and access those data members via that pointer.

Although it's technically not due to virtual inheritance, it's probably worth noting that there's a separate (again, small) penalty for multiple inheritance in general. In a typical implementation of single inheritance, you have a vtable pointer at some fixed offset in the object (quite often the very beginning). In the case of multiple inheritance, you obviously can't have two vtable pointers at the same offset, so you end up with a number of vtable pointers, each at a separate offset in the object.

IOW, the vtable pointer with single inheritance is normally just static_cast<vtable_ptr_t>(object_address), but with multiple inheritance you get static_cast<vtable_ptr_t>(object_address+offset).

Technically, the two are entirely separate -- but of course nearly the only use for virtual inheritance is in conjunction with multiple inheritance, so it's semi-relevant anyway.

Jerry Coffin
  • 455,417
  • 76
  • 598
  • 1,067
2

Concretely in Microsoft Visual C++ there is an actual difference in pointer-to-member sizes. See #pragma pointers_to_members. As you can see in that listing - the most general method is "virtual inheritance" which is distinct from multiple inheritance which in turn is distinct from single inheritance.

That implies that more information is needed to resolve a pointer-to-member in the case of presence of virtual inheritance, and it will have a performance impact if only through the amount of data taken up in the CPU cache - though likely also in the length of the lookup of the member or the number of jumps needed.

Joris Timmermans
  • 10,558
  • 2
  • 47
  • 73
1

Your question is focused mostly on calling regular functions of the virtual base, not the (far) more interesting case of virtual functions of the virtual base class (class A in your example)-- but yes, there can be a cost. Of course everything is compiler dependent.

When the compiler compiled A::foo, it assumed that "this" points to the start of where the data members for A resides in memory. At this time, the compiler might not know that class A will be a virtual base of any other class. But it happily generates the code.

Now, when the compiler compiles B, there won't really be a change because while A is a virtual base class, it is still single inheritance and in the typical case, the compiler will layout class B by placing class A's data members immediately followed by class B's data members-- so a B * can be immediately castable to a A * without any change in value, and hence, the no adjustments need to be made. The compiler can call A::foo using the same "this" pointer (even though it is of type B *) and there is no harm.

The same situation is for class C-- its still single inheritance, and the typical compiler will place A's data members immediately followed by C's data members so a C * can be immediately castable to an A * without any change in value. Thus, the compiler can simply call A::foo with the same "this" pointer (even though it is of type C*) and there is no harm.

However, the situation is totally different for class D. The layout of class D will typically be class A's data members, followed by class B's data members, followed by class C's data members, followed by class D's data members.

Using the typical layout, a D * can be immediately convertable to an A *, so there is no penalty for A::foo-- the compiler can call the same routine it generated for A::foo without any change to "this" and everything is fine.

However, the situation changes if the compiler needs to call a member function such as C::other_member_func, even if C::other_member_func is non-virtual. The reason is that when the compiler wrote the code for C::other_member_func, it assumed that the data layout referenced by the "this" pointer is A's data members immediately followed by C's data members. But that is not true for an instance of D. The compiler may need to rewrite and create a (non-virtual) D::other_member_func, just to take care of the class instance memory layout difference.

Note that this is a different but similar situation when using multiple inheritance, but in multiple inheritance without virtual bases, the compiler can take care of everything by simply adding a displacement or fixup to the "this" pointer to account for where a base class is "embedded" within an instance of a derived class. But with virtual bases, sometimes a function rewrite is needed. It all depends on what data members are accessed by the (even non-virtual) member function being called.

For example, if class C defined a non-virtual member function C::some_member_func, the compiler might need to write:

  1. C::some_member_func when called from an actual instance of C (and not D), as determined at compile time (because some_member_func isn't a virtual function)
  2. C::some_member_func when the same member function is called from an actual instance of class D, as determined at compile time. (Technically this routine is D::some_member_func. Even though the definition of this member function is implicit and identical to the source code of C::some_member_func, the generated object code may be slightly different.)

if the code for C::some_member_func happens to use member variables defined in both class A and class C.

Guest User
  • 11
  • 2
1

I think, there is no runtime penalty for virtual inheritance. Don't confuse virtual inheritance with virtual functions. Both are two different things.

virtual inheritance ensures that you've only one sub-object A in instances of D. So I don't think there would be runtime penalty for it alone.

However, there can arise cases where this sub-object cannot be known at compile time, so in such cases there would runtime penalty for virtual inheritance. One such case is described by James in his answer.

Nawaz
  • 341,464
  • 111
  • 648
  • 831
  • 1
    Actually there may be one. There is an extra pointer stored in all classes which inherits virtually from A, to locate this one subobject. In OP's case however, the compiler can statically determine where the A subobject resides. – Alexandre C. Apr 05 '11 at 15:00
  • 1
    @Alexandre: Interesting. Could you explain that a bit more? Is that implementation defined? – Nawaz Apr 05 '11 at 15:02
  • 1
    the vptr mechanism for virtual inheritance is the same as with virtual methods. Have a look at this article: [Solving the Diamond Problem with Virtual Inheritance](http://www.cprogramming.com/tutorial/virtual_inheritance.html) **EDIT**: I mean, it uses virtual pointers, which may cause a slight runtime penalty. – Assambar Apr 05 '11 at 15:08
  • *May be* this answer was correct. I have raised another [question](http://stackoverflow.com/questions/9444003/is-there-any-extra-cost-of-calling-non-virtual-base-methods-in-virtual-inheritan) though. Downvoting is not completely justified. – iammilind Feb 25 '12 at 13:31
0

There has to be a cost to virtual-inheritance.

The proof is that virtually inherited classes occupy more than the sum of the parts.

Typical case:

struct A{double a;};

struct B1 : virtual A{double b1;};
struct B2 : virtual A{double b2;};

struct C : virtual B1, virtual B2{double c;}; // I think these virtuals are not strictly necessary
static_assert( sizeof(A) == sizeof(double) ); // as expected

static_assert( sizeof(B1) > sizeof(A) + sizeof(double) ); // the equality holds for non-virtual inheritance
static_assert( sizeof(B2) > sizeof(A) + sizeof(double) );  // the equality holds for non-virtual inheritance
static_assert( sizeof(C) > sizeof(A) + sizeof(double) + sizeof(double) + sizeof(double) );
static_assert( sizeof(C) > sizeof(A) + sizeof(double) + sizeof(double) + sizeof(double) + sizeof(double));

(https://godbolt.org/z/zTcfoY)

What is stored additionally? I don't exactly understand. I think it is something like a virtual table but for accessing individual members.

alfC
  • 12,434
  • 4
  • 54
  • 98