Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Guide

Why dynamic_cast has runtime performance overhead and how typeid works internally [StackOverflow Architecture Guide]

modern_cpp_artisan
C++ Template Wizard
MEMBER
прадстаўнік: 124
Дата далучэння: Jun 2019
Паведамленні: 29
Дзякуй: 72
1 месяцаў таму · Jul 16, 2026 9:41 PM
#1

How dynamic_cast<Target*>(ptr) performs safe downcasting in polymorphic class hierarchies:

Unlike static_cast which is a zero-cost compile-time offset adjustment, dynamic_cast traverses Runtime Type Information (RTTI) metadata tables located above the virtual table pointer to verify inheritance relationships.

In deep class hierarchies with multiple inheritance, dynamic_cast performs multiple string/pointer comparisons across the type descriptor graph. In performance-critical loops, prefer tagged sum types (std::variant) or static polymorphism (CRTP).

vtable_slayer
Senior Reverser
MEMBER
прадстаўнік: 215
Дата далучэння: Mar 2018
Паведамленні: 86
Дзякуй: 61
1 месяцаў таму · Jul 17, 2026 2:50 AM
#2

Profiling a physics loop once revealed 12% CPU time spent entirely in dynamic_cast type checks. Switching to an enum tag check made it 10x faster.

profiler_pat
Performance Hunter
MEMBER
прадстаўнік: 146
Дата далучэння: Aug 2019
Паведамленні: 33
Дзякуй: 31
1 месяцаў таму · Jul 17, 2026 2:09 PM
#3

RTTI is also disabled in many game engines and embedded systems with -fno-rtti to reduce binary size and avoid virtual dispatch tables overhead.