Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual method table
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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%.<ref>Driesen, Karel and Hölzle, Urs, [https://dl.acm.org/doi/10.1145/236338.236369 "The Direct Cost of Virtual Function Calls in C++"], OOPSLA 1996</ref> The cost of virtual functions may not be so high on modern {{abbr|CPU|Central Processing Unit}} architectures due to much larger caches and better [[branch predictor|branch prediction]]. Furthermore, in environments where [[Just-in-time compilation|JIT compilation]] is not in use, virtual function calls usually cannot be [[inline expansion|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 <code>f1</code> above may not require a table lookup because the compiler may be able to tell that <code>d</code> can only hold a <code>D</code> at this point, and <code>D</code> does not override <code>f1</code>. Or the compiler (or optimizer) may be able to detect that there are no subclasses of <code>B1</code> anywhere in the program that override <code>f1</code>. The call to <code>B1::f1</code> or <code>B2::f2</code> will probably not require a table lookup because the implementation is specified explicitly (although it does still require the 'this'-pointer fixup).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)