Developer knowledge network · moderated exchange

Сообщество UnreliableCode

Сообщество разработчиков, обратного проектирования и кодирования

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.