Question

I had to design and develop a C++ module which will be used in a real time environment (it will be run on a modern multi-core PC). When I designed it I created C++ interfaces (classes with only pure virtual member functions) and I used dependency injection in order to be able to test it with Google Mock Framework. I know that this approach has run time performance overhead compered to the static binding but the testability was an important factor.

I thought we can measure the execution time during the development and also run tests at the integration phases to decide if the performance overhead is acceptable.

In the past few days I received a critics that this approach will not work because late binding has an nondeterministict nature. This nondeterministict nature means even if I test it and I measure the execution times, later, in the production environment the execution time can be more only because of the late binding (because I used pure virtual functions).

As far as I know it is not possible (with exception of the cache misses and things like that). If you use interfaces it means you will have some additional layers of indirection and the compiler cannot optimize in some cases (inline functions for example), but that's it.

So my question is not the performance overhead but the variability of the performance. Can it vary between two execution?

I cannot find any article or benchmark about this topic. The found articles benchmark the constant performance difference between the static and dynamic binding but again it is not the question now.

If you know any openly accessible articles, webpages, books, source or anything that can help, please share with me.

Thank you!

Update: I would like to put the links and documents where I found the answer:

  1. How the virtual function call works: Parashift C++ FAQ
  2. Technical Report on C++ Performance, page 87: "If the static type of an object can be determined at compile-time, calling a virtual function may be no more expensive than calling a non-virtual member function. If the type must be dynamically determined at run-time, the overhead will typically be a fixed number of machine instructions (§5.3.3) for each call."
  3. Agnes Fog's Optimizing software in C++: An optimization guide for Windows, Linux and Mac platforms, page 53: "The time it takes to call a virtual member function is a few clock cycles more than it takes to call a non-virtual member function, provided that the function call statement always calls the same version of the virtual function."
Was it helpful?

Solution

The international C++ standardization committee in 2005 published a Technical Report on C++ Performance , which I believe qualifies as both article and benchmark about this topic.

The short answer is that cache misses can influence run time considerably, and in a call of a virtual function the vtable is (usually) consulted.

But in practice (as opposed to the formal) the overhead per call in terms of executed machine code, is fixed, because all extant compiled C++ implementations use vtables. You can derive classes to your heart's content without affecting the call overhead. Any call still performs (1) look up vtable pointer in known place in object, (2) look up function address in known place in vtable, (3) call that function, unless the compiler knows that the function pointer is available from e.g. an earlier call, which just, if anything, makes the call go a wee bit faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top