Developer knowledge network · moderated exchange

مجتمع الكود غير الموثوق به

أبحاث المطورين، مجتمع الهندسة العكسية والترميز

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 months ago · 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 months ago · 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 months ago · 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.