Fast dynamic casting progress

A few years ago I found this very interesting document about a very convenient performance update for dynamic_cast in C ++: http://www2.research.att.com/~bs/fast_dynamic_casting.pdf .

Basically, this makes dynamic_cast in C ++ faster than traditional research in the inheritance tree. As stated in the document, the method provides a fast dynamic cast algorithm with constant time.

This article was published in 2005. Now I am wondering if any technique is being implemented somewhere, or are plans to implement it anywhere?

+7
source share
1 answer

I don't know which implementations the different compilers use next to GCC (which is not linear). However, it is important to emphasize that the document does not necessarily offer a method that is always faster than existing implementations for all (or even ordinary) uses. He offers a general solution that is asymptotically better as the inheritance hierarchy grows.

However, it rarely happens that good design has large inheritance hierarchies as they strive to make the application become monolithic and inflexible for change. Flexible design programs tend to have hierarchies with basically two levels, an abstract base and implementation of polymorphic run-time roles to support the Open / Closed Principle. In these cases, moving the inheritance graph can be as simple as dereferencing and comparing with a single pointer, which can be faster than comparing with the index-sum, then dereferencing, presented by Gibbs and Straustup.

In addition, it is important to emphasize that you never need to write a program that uses dynamic_cast unless your own business rules require it. Using dynamic_cast always indicates that polymorphism is not being used properly and reuse is compromised. If you need behavior based on casting a hierarchy, adding a virtual method gives a clean solution. If you have a section of code that performs dynamic_cast type checks, this section of code never "closes" (in the sense of the "Open / Closed" principle), and it will need to be updated for each new type added to the system. On the other hand, virtual dispatching is added only to new types, which allows you to remain open for expansion and still close the behavior that operates on the base type.

So, this is really quite an academic proposal (equivalent to an algorithm for changing a map to hash_map algorithmically), which should not have real effects if a good design is followed. If business rules prohibit good design (some stores may have code barriers or code problems in which you cannot modify existing architectures the way they should be, and they do not allow adapters to be created, as they are usually used for third-party libraries), then it is better not to decide which compiler to use based on which algorithm is implemented. As always, if performance is key, and you need to use a feature like dynamic_cast, your code profile. It is possible (and probably in many cases) that a tree implementation is faster in practice.

See also a review of standards committees, including dynamic_cast and the well-known C ++ look in embedded environments and good use (which mentions Gibbs and Stroustrup) .

+7
source

All Articles