Performance variability of C++ pure virtual function calls

Gabor Meszaros

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."
Cheers and hth. - Alf

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++ Virtual Function performance for x calls on the same object

From Dev

Pure virtual and override function (c++)

From Dev

Is there a pure virtual function in the C++ Standard Library?

From Dev

Pure virtual function implementation

From Dev

C++ Abstract Base Class with pure virtual operator+ function

From Dev

Adding a body to a pure virtual/abstract function in C++?

From Dev

use of pure virtual function defined outside the class in C++?

From Dev

C++ 11 Threads, Error Pure virtual function called

From Dev

Avoiding "Pure Virtual Function Call" in Derived Class C++

From Dev

SFML C++ drawing into RenderWindow pure virtual function runtime failure

From Dev

C++ passing base type to pure virtual function

From Dev

Adding a body to a pure virtual/abstract function in C++?

From Dev

C++: Avoiding repetition by calling pure virtual function

From Dev

use of pure virtual function defined outside the class in C++?

From Dev

C++ syntax of template class with pure virtual function?

From Dev

C++: Overriding virtual pure function derived from a template class

From Dev

Pure virtual function overridding virtual function

From Dev

Pure virtual function overridding virtual function

From Dev

virtual function calls and casting

From Dev

Deriving implementation of pure virtual function

From Dev

How to implement pure virtual function

From Dev

Purpose of private pure virtual function?

From Dev

Class with implementation of pure virtual function

From Dev

Swift map function variability

From Dev

C++11 std::function slower than virtual calls?

From Dev

C++ override pure virtual method with pure virtual method

From Dev

C++: call pure virtual function from member function of same class

From Java

Is a function that calls Math.random() pure?

From Dev

How to test a tree of pure function calls in isolation?

Related Related

  1. 1

    c++ Virtual Function performance for x calls on the same object

  2. 2

    Pure virtual and override function (c++)

  3. 3

    Is there a pure virtual function in the C++ Standard Library?

  4. 4

    Pure virtual function implementation

  5. 5

    C++ Abstract Base Class with pure virtual operator+ function

  6. 6

    Adding a body to a pure virtual/abstract function in C++?

  7. 7

    use of pure virtual function defined outside the class in C++?

  8. 8

    C++ 11 Threads, Error Pure virtual function called

  9. 9

    Avoiding "Pure Virtual Function Call" in Derived Class C++

  10. 10

    SFML C++ drawing into RenderWindow pure virtual function runtime failure

  11. 11

    C++ passing base type to pure virtual function

  12. 12

    Adding a body to a pure virtual/abstract function in C++?

  13. 13

    C++: Avoiding repetition by calling pure virtual function

  14. 14

    use of pure virtual function defined outside the class in C++?

  15. 15

    C++ syntax of template class with pure virtual function?

  16. 16

    C++: Overriding virtual pure function derived from a template class

  17. 17

    Pure virtual function overridding virtual function

  18. 18

    Pure virtual function overridding virtual function

  19. 19

    virtual function calls and casting

  20. 20

    Deriving implementation of pure virtual function

  21. 21

    How to implement pure virtual function

  22. 22

    Purpose of private pure virtual function?

  23. 23

    Class with implementation of pure virtual function

  24. 24

    Swift map function variability

  25. 25

    C++11 std::function slower than virtual calls?

  26. 26

    C++ override pure virtual method with pure virtual method

  27. 27

    C++: call pure virtual function from member function of same class

  28. 28

    Is a function that calls Math.random() pure?

  29. 29

    How to test a tree of pure function calls in isolation?

HotTag

Archive