[Xorp-hackers] dynamic_cast<T> refactoring candidate in RIB

Bruce Simpson bms at incunabulum.net
Sun Nov 15 01:31:21 PST 2009


I picked up on this on a drive-by code review on Friday. It might 
interest someone trying to wring a few more cycles out of the code, or 
in deploying Boost in a useful way.

The RIB has a table_has_name_and_type() function template which is 
currently unused. It could be removed.

dynamic_cast<T> is used in a number of places to check the type of a 
given RouteTable<A> at runtime. A refactoring to replace these uses of 
dynamic_cast<T> with a table_has_type() function template might be 
slightly more readable.

Because the return of this dynamic_cast<T> is used as a hint as to 
whether the object supports a given interface or not, it is not suitable 
for a Boost polymorphic_cast<T>, nor polymorphic_downcast<T>.

It could be argued that the use of dynamic_cast<T> is potentially 
expensive, although in practice, this is highly dependent upon the C++ 
compiler in use. As a rough heuristic, performance problems with 
dynamic_cast<T> are normally only a problem if the inheritance graph is 
deeper than ~4 classes.

If object pointers are used to perform this check, dynamic_cast<T> and 
RTTI must be used; virtual functions are in use. It could conceivably be 
replaced with some other mechanism e.g. a single table_type() virtual, 
although this refactoring probably isn't on a critical path.



More information about the Xorp-hackers mailing list