Virtual Dispatch and What It Costs
"What does virtual cost?" is a recurring question at the C++-first firms, and it is a good one because the obvious answer is the small part of the real answer.
The mechanism
On every mainstream implementation, a class with any virtual function gets a hidden pointer, the vptr, as its first member (one per base with virtual functions, under multiple inheritance). It points to a per-class table of function pointers, the vtable, built once at compile time. The standard mandates none of this machinery, only the observable dispatch, but no production ABI does it differently. A virtual call loads the vptr from the object, loads the entry at a fixed offset in the table, and calls through it.
That is two dependent loads and an indirect call, which on modern hardware is perhaps a couple of nanoseconds when everything is in cache, and the indirect branch predictor handles a call site that always resolves to the same target very well. Stated alone, this makes virtual dispatch sound nearly free, and for most code it is.
The rest of this lesson is for subscribers
Unlock every lesson in Systems Programming for Trading, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Systems Programming for Trading
19 lessons in this course, and every other premium course, on one subscription.
- Every lesson in every course, with the worked examples and interactive simulators
- Graded questions on every lesson, with explanations for the wrong answers as well as the right one
- The trainers, timed assessments and brainteaser library that go with them