The Cost of RTTI: An In-Depth Analysis
RTTI, or Runtime Type Information, is a feature in C that allows you to determine the type of an object at runtime. While it is a powerful tool, it comes with a certain performance overhead. However, quantifying this overhead has proven elusive.
Performance Impact
The cost of RTTI varies depending on the implementation. However, some general observations can be made.
Implementation Differences
GCC, the most widely used C compiler, has a vendor-neutral ABI that provides stable std::type_info objects across dynamic linking boundaries. This means that type comparisons using typeid(a) == typeid(b) are very fast on Linux, BSD, and other supported embedded platforms.
In contrast, mingw32-gcc follows the Windows C ABI, which does not guarantee the stability of std::type_info objects across DLLs. As a result, type comparisons in this case rely on strcmp, which is significantly slower.
Development Considerations
While RTTI provides certain advantages, it is generally advisable to avoid it for design reasons. However, if you have specific requirements that necessitate its use, understanding its performance characteristics can help you make informed decisions.
GCC-Specific Insights
In GCC, the use of RTTI increases the binary size of a simple test program by a few hundred bytes. This may seem counterintuitive, but it is likely due to adjustments made within the STL code in the absence of RTTI.
Conclusion
The cost of RTTI is implementation-specific and should be carefully considered when making design choices. By understanding the underlying mechanics and performance implications, developers can make informed decisions about whether and how to employ RTTI in their applications.
The above is the detailed content of What is the Performance Cost of Runtime Type Information (RTTI) in C ?. For more information, please follow other related articles on the PHP Chinese website!