↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

The Performance Benefits of Final Classes

The article says final marks a class or virtual member function as one that cannot be derived from or overridden. It argues that this helps the compiler enforce design intent and can also improve performance by enabling devirtualization.

Reading notes
#

  • final prevents further derivation from a class or overriding of a virtual member function.
  • The compiler can use final to identify more virtual calls that can be resolved at compile time.
  • Virtual calls use an indirect call through the vtable, which is more expensive than a direct call because of branch prediction, instruction cache, and missed inlining opportunities.
  • Devirtualization resolves virtual calls at compile time instead of runtime.
  • In the example with dog::speak, compilers inline the call into main because the only possible result is known.
  • In the derived final : base example, a call to d.f() can be inlined because derived cannot be further derived.
  • Without final, the assembly shown uses a vtable load and an indirect jump.
  • The article says the compiler may optimize more aggressively when it can see the full code path that will execute.
  • The conclusion recommends checking whether codebases have places where final could improve performance and measuring the impact.