I watch the lecture Intro to the C++ Object Model from Richard Powell today to refresh my understanding of the virtual functions in C++. Here are some thoughts on the overhead of the C++ virtual functions.
The runtime overhead of the virtual functions is caused by an extra indirection for the function call. This overhead is tiny in terms of a single function call, but it would be a major performance problem if you put a virtual call inside a deep inner loop.
I suggest to do dynamic dispatch as early as possible so that we can eliminate virtual function calls inside the loop.
On the other hand, the space overhead of the virtual functions comes from the extra virtual pointer for each class instance, and possible paddings/alignments(note: compiler specific). So it is not suggested to use dynamic bindings for objects of large population, such as particle system. I prefer the POD type(usually with structure-of-array) for this case.
Sometimes we want to ensure that no object is sliced
and to use POD type for optimal performance,
i.e. no virtual function calls, even a single virtual destructor,
for this case we can mark the class as
to enforce this concept at compile time.