Access to virtual functions much slower

P

parag

For the a given hierarchy

class A{
virtual bool isUselessFunc() = 0;
};


class B : public A {
bool isUselessFunc() {
return true;
}
};

class C : public B
{
bool OtherFunc() {
return false;
}
};


Today if we have a pointer to a Object of Class C , ( in a pointer of
A )
C * c = new C ():
A* a = c;

I need to acces c->isUselessFunc() many times in a code , I see that
in the collect report , it is one of the top most used functions.
Is there any way we could reduce this footprint, but having all the
class deriving from Class B have their own non virtual version ???
 
V

Victor Bazarov

For the a given hierarchy

class A{
virtual bool isUselessFunc() = 0;
};


class B : public A {
bool isUselessFunc() {
return true;
}
};

class C : public B
{
bool OtherFunc() {
return false;
}
};


Today if we have a pointer to a Object of Class C , ( in a pointer of
A )
C * c = new C ():
A* a = c;

I need to acces c->isUselessFunc() many times in a code , I see that
in the collect report ,

What's "the collect report"? I am just not familiar with the term, sorry.
> it is one of the top most used functions.
Is there any way we could reduce this footprint, but having all the
class deriving from Class B have their own non virtual version ???

You see that it's the most used. Do you see that it's the bottleneck,
or are you just guessing? Are you trying to improve the performance of
something that matters or something that doesn't really contribute to
the run time?

First of all, you need to *measure* performance, not guess.

If your model requires polymorphic behavior, you're stuck with virtual
functions, I'm afraid. In all fairness, it's just a couple extra
lookups in a table, and usually it's quite fast (a few CPU instructions
if anything).

V
 
C

Carlo Milanesi

parag said:
For the a given hierarchy

class A{
virtual bool isUselessFunc() = 0;
};


class B : public A {
bool isUselessFunc() {
return true;
}
};

class C : public B
{
bool OtherFunc() {
return false;
}
};


Today if we have a pointer to a Object of Class C , ( in a pointer of
A )
C * c = new C ():
A* a = c;

I need to acces c->isUselessFunc() many times in a code , I see that
in the collect report , it is one of the top most used functions.
Is there any way we could reduce this footprint, but having all the
class deriving from Class B have their own non virtual version ???

I am not sure to have understood your English, but perhaps what you are
searching for is to replace the following call:
c->isUselessFunc()
with the following one:
c->B::isUselessFunc()
The latter call avoids the virtual function call overhead, and it may be
inlined to avoid the function call at all.
 
D

Duc Le

I am not sure to have understood your English, but perhaps what you are
searching for is to replace the following call:
c->isUselessFunc()
with the following one:
c->B::isUselessFunc()
The latter call avoids the virtual function call overhead, and it may be
inlined to avoid the function call at all.

c->B::isUselessfunc() would only work if c is actually derived from B.
Since c is statically typed to A*, it may or may not have been derived
from B.

Duc Le
 
V

Victor Bazarov

c->B::isUselessfunc() would only work if c is actually derived from B.
Since c is statically typed to A*, it may or may not have been derived
from B.

Uh... No, in the original post 'c' is declared as C*.

V
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top