Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Guide

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

modern_cpp_artisan
C++ Template Wizard
MEMBER
Rep: 124
Datum pridruživanja: Jun 2019
Postovi: 29
Hvala: 72
prije 1 mjeseci · 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
Rep: 215
Datum pridruživanja: Mar 2018
Postovi: 86
Hvala: 61
prije 1 mjeseci · 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
Rep: 146
Datum pridruživanja: Aug 2019
Postovi: 33
Hvala: 31
prije 1 mjeseci · 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.