views:

99

answers:

6

As described in http://stackoverflow.com/questions/449827/virtual-functions-and-performance-c virtual methods may have an impact on performance (extra lookup in vtable, no inlining, ...).

But I was wondering, could the use of virtual functions speed up the linking process?

Suppose that I have a class X, calling a method of class Y.

  • If the method is a non-virtual method, then
    • the compiler has to look up the method in class Y to see whether it is valid, and how the call should be translated to assembly
    • the linker has to find the method in class Y and replace the call address in the compiler-generated assembly by the address of the called method.
  • If the method is a virtual method, then
    • the compiler will also have to look up the method in class Y, and has to look up the vtable of class Y to construct the call (using the offset in the vtable)
    • the linker has to do nothing anymore

It seems to me that when using virtual methods, the linker doesn't have to do much anymore, and therefore it will be faster (although I think the difference will be small).

Is this true? Does someone have experience with this? Was this ever tested?

A: 

No. Also, don't do it for the reasons you've given.

There may be some time saved during the dynamic linking, but it's negligible compared with the overhead and additional hassle of virtual methods. Dynamic linking is a one-time cost, virtual methods will cost you for the entire lifetime of the process.

Matt Joiner
A: 

In theory, the linker doesn't have to do as much for a virtual function call. However, I tend to doubt if this will have much effect in practice. Consider how much work the linker has to do just to get rid of all the unreferenced code (which tends to be quite large in C++).

If link time really is a problem, your efforts might be better spent making DLLs/shared libraries, for instance, instead of statically linked code.

+1  A: 

I don't get it, doesn't the compiler go through the exact same process with virtual calls as non virtual calls only it has to look up where the method would be in the virtual table (as well as hold the virtual table in memory thus ruining locality) and handle generating code for the indirect call also?

If anything, virtual calls slow down compilation time.

Blindy
+3  A: 

The design of your software should not be influenced by the speed of compiling or linking, but by common sense and pertinence !

Sorry if I'm rude here, but trying to win a few seconds of compilation, at the cost of having a poor design is a really bad idea imho.

ereOn
I completely agree, and I am not planning on changing the implementation just to gain 2 seconds of linking time. I was just wondering if it would have an effect. I was not considering actually doing this.
Patrick
By the way, you are not rude, just clear :-)
Patrick
@Patrick: That's nice ;) I didn't wanted to seem rude.
ereOn
A: 

If the class hierarchy is big, the linker must gather constructor symbols. If your classes are not polymorphic, constructors are often inlined, whereas if they are, constructors are always emitted: they have to set up the vtable, etc.

So what you would (marginally) gain with methods are likely to be offset by setting up constructors and destructors.

Alexandre C.
A: 

You're forgetting that the linker needs to put the function addresses in the vtables, and the vtables in the executable.

MSalters