Devirtualization and Static Polymorphism

C++ devs bicker over speed tricks as Rust fans smirk

TLDR: The article says cutting “virtual calls” can speed code by resolving decisions at compile time. Comments erupt: C++ template fans say modern tools make old patterns unnecessary, Rust folks brag about “fat pointers,” and performance nerds cite papers, all agreeing speed matters but fighting over how to get it.

This post on making code faster lit up the comments: the author says “virtual calls” (think: calling a friend through an assistant instead of directly) slow things down, and shows how to ditch them or let the compiler guess the real target. The community immediately split into camps: Team Static cheering compile-time decisions, and Team Virtual defending classic object-oriented design. Enter the “well actually” brigade: pjmlp swoops in to say modern C++ features like concepts (compile-time rules) and templates mean there’s no need for CRTP, the old pattern folks use for zero-cost abstraction. Meanwhile, hinkley drops a nerd-bomb about a paper claiming interfaces can be as fast or faster than vtables by using perfect hashing—cue the “show me the benchmarks” chant. Rust arrives fashionably late via gignico, flexing that Rust’s fat pointers (pointers that carry extra type info) shift the cost away from each object, so you only pay when you opt into dynamic behavior. Translation: Rust fans smirk, C++ purists sip coping tea. TimorousBestie just vibes, praising the rare plain-English walkthrough. The memes? “Vtables are middle managers,” “final = fired,” and “turn on -flto, slim your code.” Drama level: high, benchmarks demanded, egos calibrated.

Key Points

  • Virtual dispatch in C++ adds overhead via vtables/vptrs, pointer indirection, larger objects, reduced inlining, and potential branch/cache penalties.
  • Compilers can devirtualize calls when the concrete type is known, converting virtual calls into direct calls.
  • Traditional per-translation-unit compilation limits cross-file devirtualization; link-time optimization (-flto) and -fwhole-program improve optimization scope.
  • Marking methods as final allows compilers to emit direct calls even when the base declares them virtual.
  • Replacing dynamic dispatch with static polymorphism can eliminate runtime cost on latency-sensitive code paths.

Hottest takes

"there is no need for CRTP" — pjmlp
"interfaces as fast or faster than vtables" — hinkley
"Rust has an interesting approach where the single objects do not pay any cost" — gignico
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.