G
George2
Hello everyone,
I want to know why virtual function's footprint and performance is
compared with normal function call.
Here is my analysis, please review whether I am correct. Thanks.
1. foorprint
Class Foo instance will consume 12 bytes other than 8 bytes to hold an
additonal __vfptr pointing to the entry address of virtual function
table. If we remove virtual keyword, the footprint of class Foo
instance will consume only 8 bytes.
2. performance
2.1 Virtual function call's performance is worse than normal function
call is because it needs to use __vfptr to find the virtual function
table, then invokes the function. Here using __vfptr brings one more
level of indirection compared with normal function call, which is the
most important reason making the performance worse.
2.2 Are there anything else matters performance?
2.3 How to find normal function call address can be determined during
compile time and virtual function call address can only be determined
during runtime?
Please comment whether my understanding of 1 and 2 are correct or not?
thanks in advance,
George
I want to know why virtual function's footprint and performance is
compared with normal function call.
Here is my analysis, please review whether I am correct. Thanks.
1. foorprint
Code:
class Foo
{
protected:
char * pName;
int nSize;
virtual void copyName(char* pN) {return;}
void virFunc() {return;}
};
Class Foo instance will consume 12 bytes other than 8 bytes to hold an
additonal __vfptr pointing to the entry address of virtual function
table. If we remove virtual keyword, the footprint of class Foo
instance will consume only 8 bytes.
2. performance
2.1 Virtual function call's performance is worse than normal function
call is because it needs to use __vfptr to find the virtual function
table, then invokes the function. Here using __vfptr brings one more
level of indirection compared with normal function call, which is the
most important reason making the performance worse.
2.2 Are there anything else matters performance?
2.3 How to find normal function call address can be determined during
compile time and virtual function call address can only be determined
during runtime?
Please comment whether my understanding of 1 and 2 are correct or not?
thanks in advance,
George