Virtual Inheritance Performance Impact

C

chsalvia

Is virtual inheritance ambiguity resolution ever performed statically,
i.e. at compiler time? Or is it always resolved at runtime?

In other words, if you use virtual inheritance, but don't use
polymorphism (pointers to derived classes), is there any overhead?
Take the following example:

class Base {
public:
int x;
void func1() { x = 2; }
};

class A : public virtual Base {
public:
int y;
};

class B : public virtual Base {
public:
int z;
};

class Derived : public A, public B {
public:
};

int main() {
Derived d;
d.func1(); // does this function call entail additional
overhead?
}
 
M

Markus Schoder

class Base {
public:
int x;
void func1() { x = 2; }
};

class A : public virtual Base {
public:
int y;
};

class B : public virtual Base {
public:
int z;
};

class Derived : public A, public B {
public:
};

int main() {
Derived d;
d.func1(); // does this function call entail additional
overhead?
}

Here is the generated assembler code of g++-4.2:

main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
leaq -32(%rbp), %rdi
call _ZN7DerivedC1Ev # This call only with virtual inheritance
leaq -32(%rbp), %rax
leaq 28(%rax), %rdi
call _ZN4Base5func1Ev # call member function
movl $0, %eax
leave
ret

So there is additional overhead.

On the other hand with optimization enabled the result is:

main:
xorl %eax, %eax
ret

Probably it is possible to create an example that cannot optimize away
the additional overhead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top