是我的避免dynamic_cast<>快于dynamic_cast<>本身? [英] Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself?
问题描述
dynamic_cast的实现当然会因编译器而异。/ p>
在Visual C ++中,vtable指向包含结构的所有RTTI的结构。因此,dynamic_cast包括解引用此指针,并根据请求的类型检查实际类型,如果它们不兼容,则抛出异常(或返回NULL)。它基本上等同于你描述的系统。这不是特殊的慢。
您的设计也听起来有点 - 你有一个工厂方法,忘记一个对象的真实类型,然后你立即想忘记该信息。也许你应该移动逻辑,当你忘记一个类型进入工厂方法,或基础类本身的虚拟方法。
I was answering a question a few minutes ago and it raised to me another one:
In one of my projects, I do some network message parsing. The messages are in the form of:
[1 byte message type][2 bytes payload length][x bytes payload]
The format and content of the payload are determined by the message type. I have a class hierarchy, based on a common class Message
.
To instantiate my messages, i have a static parsing method which gives back a Message*
depending on the message type byte. Something like:
Message* parse(const char* frame)
{
// This is sample code, in real life I obviously check that the buffer
// is not NULL, and the size, and so on.
switch(frame[0])
{
case 0x01:
return new FooMessage();
case 0x02:
return new BarMessage();
}
// Throw an exception here because the mesage type is unknown.
}
I sometimes need to access the methods of the subclasses. Since my network message handling must be fast, I decived to avoid dynamic_cast<>
and I added a method to the base Message
class that gives back the message type. Depending on this return value, I use a static_cast<>
to the right child type instead.
I did this mainly because I was told once that dynamic_cast<>
was slow. However, I don't know exactly what it really does and how slow it is, thus, my method might be as just as slow (or slower) but far more complicated.
What do you guys think of this design ? Is it common ? Is it really faster than using dynamic_cast<>
? Any detailed explanation of what happen under the hood when one use dynamic_cast<>
is welcome !
--- EDIT ---
Since some people asked why:
Basically, when I receive a frame, I do two things:
- I parse the message and build a corresponding instance of a subclass of
Message
if the content of the frame is valid. There is no logic except for the parsing part. - I receive a
Message
and depending on aswitch(message->getType())
, Istatic_cast<>
to the right type and do whatever has to be done with the message.
Implementations of dynamic_cast will of course vary by compiler.
In Visual C++, the vtable points to a structure which contains all of the RTTI about a structure. A dynamic_cast therefore involves dereferencing this pointer, and checking the "actual" type against the requested type, and throwing an exception (or returning NULL) if they are not compatible. It is basically equivalent to the system you describe. It's not particularity slow.
Your design also sounds a bit off - you have a factory method which forgets the true type of an object, then you immediately want to un-forget that information. Perhaps you should move that logic you do when unforgetting a type into the factory method, or into virtual methods on the base class itself.
这篇关于是我的避免dynamic_cast<>快于dynamic_cast<>本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!