skip to content

When can a C++ compiler turn a virtual update() call into a direct call, and why does marking Particle final not help calls through Body&?

level: seniorimportance: should knowfreq 45%

answer

  1. prove the dynamic type
  2. the static type is final
  3. local object or fresh allocation
  4. whole program under LTO
  5. speculative: guess, check, fall back

basics

~20 s

When it can prove the override (final static type, visible dynamic type, one implementation program-wide) or guards a guess with a runtime check. Through Body&, final on Particle proves nothing: any class derived from Body could be there.

solid answer

~50 s

**Devirtualization** replaces an indirect call with a direct, inlinable one. An unconditional direct call needs a **proof** of which override runs, which the compiler has when the **static type is a final class**, or the function is `final` in that type; when the **dynamic type is visible**, as for a local object, and often for one just created with `new`; and when **whole-program analysis** shows only one implementation, which Clang does with `-flto -fwhole-program-vtables` for classes with hidden LTO visibility. GCC also **speculates**: `-fdevirtualize-speculatively`, on at `-O2`, emits a guarded direct call to a likely target and falls back to the indirect call. Marking `Particle` final does not help `void step(Body& b)`, because the static type there is `Body`, and `Rigid`, `Cloth` or a class from another library could be behind the reference. Final helps where code names the leaf type.

code

cpp · 23 lines
cpp
#include <cstdio>

struct Body {
    virtual ~Body() = default;
    virtual void update(float dt) = 0;
    float x = 0, v = 1;
};
struct Particle final : Body {
    void update(float dt) override { x += v * dt; }
};
struct Rigid : Body {
    void update(float dt) override { x += v * dt; v *= 0.99f; }
};

void stepAny(Body& b, float dt)          { b.update(dt); }  // indirect call
void stepParticle(Particle& p, float dt) { p.update(dt); }  // final class: direct, inlined
void stepRigid(Rigid& r, float dt)       { r.update(dt); }  // Rigid not final: indirect

int main() {
    Particle p; Rigid r;
    stepAny(p, 0.5f); stepParticle(p, 0.5f); stepRigid(r, 0.5f);
    std::printf("%g %g\n", p.x, r.v);
}

go deeper

for a junior

Know that final lets the compiler call a function directly when it knows the exact class, which can make code faster.

for a middle

List the proofs the compiler needs: final static type or function, visible dynamic type, qualified call, and explain why a base reference defeats final.

for a senior

Use the tooling: whole-program devirtualization under LTO and hidden visibility, GCC's speculative devirtualization, the suggest-final warnings, and reading -O2 output to confirm.

for a principal

Decide the build and code conventions that make devirtualization dependable: final leaf classes, LTO with hidden visibility, and explicit public visibility for extensible hierarchies.

## What devirtualization is A virtual call through `Body&` compiles to loads and an indirect branch, and the optimiser cannot inline through it. **Devirtualization** is the optimisation that replaces it with a **direct call**, which can then be inlined, constant-folded and vectorised like any other. The standard permits it because the result is the same: the call still runs the final overrider for the dynamic type. The compiler needs either a **proof** of which function that is, or a runtime check that guards its guess. ## When the proof is available - **The static type is a `final` class.** If `Particle` is `final`, a `Particle&` can only refer to a `Particle`, so `p.update(dt)` calls `Particle::update` directly. - **The function is `final` in the static type.** `void update(float) final` in `Rigid` lets calls through `Rigid&` go direct even if other classes derive from `Rigid`. - **The dynamic type is visible.** For a local `Rigid r; r.update(dt);` the type is known at the call, and for an object created with `new Rigid` in the same function the optimiser can often track it. - **A qualified call.** `b.Body::update(dt)` names the function explicitly and is never dispatched. - **Whole-program knowledge.** If the optimiser sees every class that derives from `Body` and only one overrides `update`, it can call that override directly. A Clang `-O2` build on arm64 shows the first case: `stepAny(Body&)` and `stepRigid(Rigid&)` each compile to `ldr`, `ldr`, `br` through the vtable, while `stepParticle(Particle&)` with a final `Particle` is inlined to a single fused multiply-add. ## Why final on Particle does not help through Body& In `void step(Body& b, float dt) { b.update(dt); }` the static type is `Body`. `final` on `Particle` says nothing derives from `Particle`; it says nothing about `Rigid`, `Cloth`, or a class compiled into another shared library that derives from `Body`. Within one translation unit, the compiler cannot rule those out. So `final` pays off at **call sites that name the leaf type**: batched loops over `std::vector<Particle>`, functions taking `Particle&`, and member functions of `Particle` calling its own virtuals. ## Whole-program and speculative devirtualization | Tool | What it does | Requirement | |---|---|---| | Clang `-fwhole-program-vtables` | single-implementation devirtualization and virtual constant propagation | `-flto`, and classes with **hidden LTO visibility** | | GCC `-fdevirtualize` | converts virtual calls to direct calls, within a function and across functions | on at `-O2`, `-O3`, `-Os` | | GCC `-fdevirtualize-speculatively` | a guarded direct call to a likely target, with the indirect call as fallback | on at `-O2` and above | | GCC `-Wsuggest-final-types`, `-Wsuggest-final-methods` | warn where `final` would let it devirtualize more | most effective with LTO | | Clang `-fstrict-vtable-pointers` | assumes the vptr does not change during an object's lifetime | experimental, off by default | Hidden LTO visibility matters because a class that a shared library can extend may have overrides the link never sees. Clang derives it from symbol visibility: with `-fvisibility=hidden` and LTO, the classes are treated as closed. On this machine, a two-file program with one implementation of `update` kept two indirect calls under `-O2 -flto -fvisibility=hidden`, and none once `-fwhole-program-vtables` was added. The Clang 23 manual also documents a `-fdevirtualize-speculatively` option; Apple clang 21 ignores it with a warning that it is not supported. Speculative devirtualization is a bet: the compiler compares the loaded function pointer, or the vptr, with the expected one and calls the expected function directly, often inlined, when they match. It pays off when one target dominates and costs a compare when it does not. ## Making a codebase devirtualization-friendly 1. Mark **leaf classes `final`** by default; it documents intent and costs nothing. 2. Write hot loops over **concrete, final types** rather than over base pointers. 3. Keep implementation classes in an **unnamed namespace** where possible; GCC's documentation names this as another way to let it devirtualize. 4. Use **LTO** with hidden visibility for the final binary, and keep classes that plugins extend visibly public. 5. Check the output: `-O2 -S` or a disassembler shows whether a call is still indirect. Devirtualization is an optimisation, never a guarantee: a compiler upgrade, a new derived class or a change of visibility can quietly turn a direct call back into an indirect one. Where a hot path depends on it, keep the proof in the source, a final type at the call site, rather than in the optimiser's analysis.

  • Why does -fwhole-program-vtables need classes with hidden LTO visibility?
    Because the optimisation assumes it sees every class in the hierarchy. A class with public visibility could be extended by a shared library loaded later, adding an override the link never saw, so Clang applies whole-program devirtualization only to classes it can treat as closed, which it derives from hidden symbol visibility under LTO.
  • Does marking Rigid::update final help if classes still derive from Rigid?
    Yes, for calls whose static type is `Rigid` or a class derived from it: no derived class may override `update`, so `r.update(dt)` through `Rigid&` can be a direct call. It still does nothing for calls through `Body&`, where another override could be behind the reference.

  • Marking the leaf class final devirtualizes every call through the base reference.
  • Compilers never devirtualize without link-time optimisation.
  • Speculative devirtualization calls the guessed function without checking the type.
  • -fwhole-program-vtables works on its own, without -flto.
  • A local object's virtual calls always go through the vtable.