Xufan Lu and Nuno P. Lopes
A core design principle of C++ is that users should only incur costs for features they actually use, both in terms of performance and code size. A notable exception to this rule is the run-time type information (RTTI) data, used for dynamic downcasts, exceptions, and run-time type introspection.
For classes that define at least one virtual method, compilers generate RTTI data that uniquely identifies the type, including a string for the type name. In large programs with complex type inheritance hierarchies, this RTTI data can grow substantially in size. Moreover, dynamic casting algorithms are linear in the type hierarchy size, causing some programs to spend considerable time on these casts.
The common workaround is to use the -fno-rtti compiler flag, which disables RTTI data generation. However, this approach has significant drawbacks, such as disabling polymorphic exceptions and dynamic casts, and requiring the flag to be applied across the entire program due to ABI changes.
In this paper, we propose a new link-time optimization to mitigate both the performance and size overhead associated with dynamic casts and RTTI data. Our optimization replaces costly library calls for downcasts with short instruction sequences and eliminates unnecessary RTTI data by modifying vtables to remove RTTI slots. Our prototype, implemented in the LLVM compiler, demonstrates an average of 1.4%, as well as an average binary size reduction of 1.7%.
X. Lu and N. P. Lopes. Link-Time Optimization of Dynamic Casts in C++ Programs. Proc. of the ACM on Programming Languages, Volume 9 Issue PLDI, June 2025.
@article{dyncast-pldi25, title = {{Link-Time} Optimization of Dynamic Casts in C++ Programs}, author = {Xufan Lu and Nuno P. Lopes}, journal = {Proc. of the ACM on Programming Languages}, volume = {9}, number = {PLDI}, publisher = {ACM}, doi = {10.1145/3729266}, month = jun, year = 2025 }
© The Authors, 2025. Licensed under the Creative Commons Attribution 4.0 International License.