Implementation
An object's virtual method table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's virtual method table. The virtual method table is the same for all objects belonging to the same class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have virtual method tables with the same layout: the address of a given method will appear at the same offset for all type-compatible classes. Thus, fetching the method's address from a given offset into a virtual method table will get the method corresponding to the object's actual class. The C++ standards do not mandate exactly how dynamic dispatch must be implemented, but compilers generally use minor variations on the same basic model. Typically, the compiler creates a separate virtual method table for each class. When an object is created, a pointer to this table, called the virtual table pointer, vpointer or VPTR, is added as a hidden member of this object. As such, the compiler must also generate "hidden" code in theExample
Consider the following class declarations in C++ syntax:b2
:G++'s -fdump-class-hierarchy
(starting with version 8: -fdump-lang-class
) argument can be used to dump virtual method tables for manual inspection. For AIX VisualAge XlC compiler, use -qdump_class_hierarchy
to dump class hierarchy and virtual function table layout.
b2: +0: pointer to virtual method table of B2 +4: value of int_in_b2 virtual method table of B2: +0: B2::f2()and the following memory layout for the object
d
:
d: +0: pointer to virtual method table of D (for B1) +4: value of int_in_b1 +8: pointer to virtual method table of D (for B2) +12: value of int_in_b2 +16: value of int_in_d Total size: 20 Bytes. virtual method table of D (for B1): +0: B1::f1() // B1::f1() is not overridden virtual method table of D (for B2): +0: D::f2() // B2::f2() is overridden by D::f2() // The location of B2::f2 is not in the virtual method table for DNote that those functions not carrying the keyword
virtual
in their declaration (such as fnonvirtual()
and d()
) do not generally appear in the virtual method table. There are exceptions for special cases as posed by the default constructor.
Also note the virtual destructors in the base classes, B1
and B2
. They are necessary to ensure delete d
can free up memory not just for D
, but also for B1
and B2
, if d
is a pointer or reference to the types B1
or B2
. They were excluded from the memory layouts to keep the example simple.
Overriding of the method f2()
in class D
is implemented by duplicating the virtual method table of B2
and replacing the pointer to B2::f2()
with a pointer to D::f2()
.
Multiple inheritance and thunks
The g++ compiler implements the multiple inheritance of the classesB1
and B2
in class D
using two virtual method tables, one for each base class. (There are other ways to implement multiple inheritance, but this is the most common.) This leads to the necessity for "pointer fixups", also called thunks, when casting.
Consider the following C++ code:
d
and b1
will point to the same memory location after execution of this code, b2
will point to the location d+8
(eight bytes beyond the memory location of d
). Thus, b2
points to the region within d
that "looks like" an instance of B2
, i.e., has the same memory layout as an instance of B2
.
Invocation
A call tod->f1()
is handled by dereferencing d
's D::B1
vpointer, looking up the f1
entry in the virtual method table, and then dereferencing that pointer to call the code.
Single inheritance
In the case of single inheritance (or in a language with only single inheritance), if the vpointer is always the first element in d
(as it is with many compilers), this reduces to the following pseudo-C++:
*d
refers to the virtual method table of D
and /code> refers to the first method in the virtual method table. The parameter d
becomes the "this
" pointer to the object.
Multiple inheritance
In the more general case, calling B1::f1()
or D::f2()
is more complicated:
(*(*(d 0*pointer to virtual method table of D (for B1)*/) )(d) /* Call d->f1() */
(*(*(d 8*pointer to virtual method table of D (for B2)*/) )(d+8) /* Call d->f2() */
The call to d->f1()
passes a B1
pointer as a parameter. The call to d->f2()
passes a B2
pointer as a parameter. This second call requires a fixup to produce the correct pointer. The location of B2::f2
is not in the virtual method table for D
.
By comparison, a call to d->fnonvirtual()
is much simpler:
(*B1::fnonvirtual)(d)
Efficiency
A virtual call requires at least an extra indexed dereference and sometimes a "fixup" addition, compared to a non-virtual call, which is simply a jump to a compiled-in pointer. Therefore, calling virtual functions is inherently slower than calling non-virtual functions. An experiment done in 1996 indicates that approximately 6–13% of execution time is spent simply dispatching to the correct function, though the overhead can be as high as 50%. The cost of virtual functions may not be so high on modern architectures due to much larger caches and better branch prediction.
Furthermore, in environments where JIT compilation
In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is a way of executing computer code that involves compilation during execution of a program (at run time) rather than before execution. This may cons ...
is not in use, virtual function calls usually cannot be inlined. In certain cases it may be possible for the compiler to perform a process known as ''devirtualization'' in which, for instance, the lookup and indirect call are replaced with a conditional execution of each inlined body, but such optimizations are not common.
To avoid this overhead, compilers usually avoid using virtual method tables whenever the call can be resolved at compile time.
Thus, the call to f1
above may not require a table lookup because the compiler may be able to tell that d
can only hold a D
at this point, and D
does not override f1
. Or the compiler (or optimizer) may be able to detect that there are no subclasses of B1
anywhere in the program that override f1
. The call to B1::f1
or B2::f2
will probably not require a table lookup because the implementation is specified explicitly (although it does still require the 'this'-pointer fixup).
Comparison with alternatives
The virtual method table is generally a good performance trade-off to achieve dynamic dispatch, but there are alternatives, such as binary tree dispatch
Binary may refer to:
Science and technology Mathematics
* Binary number, a representation of numbers using only two digits (0 and 1)
* Binary function, a function that takes two arguments
* Binary operation, a mathematical operation that t ...
, with higher performance but different costs.Zendra, Olivier and Driesen, Karel
"Stress-testing Control Structures for Dynamic Dispatch in Java"
pp. 105–118, Proceedings of the USENIX 2nd Java Virtual Machine Research and Technology Symposium, 2002 (JVM '02)
However, virtual method tables only allow for single dispatch on the special "this" parameter, in contrast to multiple dispatch (as in CLOS, Dylan, or Julia), where the types of all parameters can be taken into account in dispatching.
Virtual method tables also only work if dispatching is constrained to a known set of methods, so they can be placed in a simple array built at compile time, in contrast to duck typing languages (such as Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
, Python or JavaScript).
Languages that provide either or both of these features often dispatch by looking up a string in a hash table, or some other equivalent method. There are a variety of techniques to make this faster (e.g., interning/tokenizing method names, caching lookups, just-in-time compilation).
See also
* Virtual function
* Virtual inheritance
* Branch table
Notes
References
* Margaret A. Ellis and Bjarne Stroustrup (1990) The Annotated C++ Reference Manual. Reading, MA: Addison-Wesley. ()
{{DEFAULTSORT:Virtual Method Table
Method (computer programming)
Articles with example C++ code