473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Performance and footprint of virtual function

200 100+
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

Expand|Select|Wrap|Line Numbers
  1. class Foo
  2. {
  3. protected:
  4.         char * pName;
  5.         int nSize;
  6.         virtual void copyName(char* pN) {return;}
  7.         void virFunc() {return;}
  8. };
  9.  
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
Jan 31 '08 #1
4 2423
weaknessforcats
9,208 Expert Mod 8TB
1. is correct.

2.1 is silly. The virtual function is called by:

obj->vtblptr->func

Using a function pointer is;

fptr->funct

Using a funciton pointer argument to call a function:

ptr ->copy as arg->call func

So you can say that virtual functions are worse than a normal function call becuse of the added layer of indirection. Please note this applies to ALL function arguments that are pointers since they have to be copied and dereferenced later.

The most you can say is using a pointer is slower than not using a pointer.

The icing on this cake is that ALL functions are called by address (i.e. a pointer).

2.3 is incorrect. All function addresses are known at compile time. Face it, if they are unknown then you can't link.
Jan 31 '08 #2
George2
200 100+
Thanks for your great reply, weaknessforcats!

1.


Using a function pointer is;

fptr->funct

Using a funciton pointer argument to call a function:

ptr ->copy as arg->call func
I do not quite understand what is the type of fptr and funct? What do you mean "ptr ->copy as arg->call func"? Could you show some simple pseudo code please?

2.

2.3 is incorrect. All function addresses are known at compile time. Face it, if they are unknown then you can't link.
Do not agree. The type can not be determined during compile time, so compile could only pass pointer to the base address of virtual function table. Compiler can not find the exact function to be invoked during compile time (since it is virtual).


regards,
George
Feb 1 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Georege2, take a deep breath and relax.
I do not quite understand what is the type of fptr and funct? What do you mean "ptr ->copy as arg->call func"? Could you show some simple pseudo code please?
What I meant to convey was that all functions are called by address. You always use a function pointer to call a funcion. That is, the name of a function is the address of the function. To call, you just jump to that address.

If you use a function pointer variable, you just jump th the address in the variable.

It's a single jump.

Overhead is 0.


Do not agree. The type can not be determined during compile time, so compile could only pass pointer to the base address of virtual function table. Compiler can not find the exact function to be invoked during compile time (since it is virtual).
You don't know what you are talking about. The virtual function table contains the addresses of the class virtual functions, which are known at compile time by the compiler. Otherwise, the virtual table couldn't be built.

When you call a virtual function, the compiler has inserted code to access the virtual funciton table to a particular offset for that function and just jumps to the function at that address.

It's a simple function pointer operation.

BTW: No types can be discovered at run time. All you have are the compiled bits.
Feb 1 '08 #4
George2
200 100+
Thanks weaknessforcats,


I have understood your points now. My question is answered.

Georege2, take a deep breath and relax.


What I meant to convey was that all functions are called by address. You always use a function pointer to call a funcion. That is, the name of a function is the address of the function. To call, you just jump to that address.

If you use a function pointer variable, you just jump th the address in the variable.

It's a single jump.

Overhead is 0.




You don't know what you are talking about. The virtual function table contains the addresses of the class virtual functions, which are known at compile time by the compiler. Otherwise, the virtual table couldn't be built.

When you call a virtual function, the compiler has inserted code to access the virtual funciton table to a particular offset for that function and just jumps to the function at that address.

It's a simple function pointer operation.

BTW: No types can be discovered at run time. All you have are the compiled bits.

regards,
George
Feb 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
2
by: D | last post by:
Hello I'm learning C++ and having done some Asm and C. I would like input on the following. If I understand what I'm reading then Objects of a type of class gets a copy of it's class' (including...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
7
by: tman | last post by:
I am generating a very large tree list in my program and while it's performance is great once loaded it takes a really long time to load. I create a root TreeNode "offline" and go through the...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
13
by: atlaste | last post by:
Hi, I'm currently developing an application that uses a lot of computational power, disk access and memory caching (to be more exact: an information retrieval platform). In these kind of...
7
by: developer28 | last post by:
Hi, I am looking for some performance monitoring tools and was suggested to ask you for the same. Can anyone please provide some help on the same? What i basically want is a tool that can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.