是我的避免dynamic_cast<>快于dynamic_cast<>本身? [英] Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself?

查看:163
本文介绍了是我的避免dynamic_cast<>快于dynamic_cast<>本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在回答

  [1字节消息类型] [2字节有效负载长度] [x字节有效负载] 

有效负载的格式和内容由消息类型决定。我有一个类层次结构,基于一个常见的类 Message



要实例化我的消息,根据消息类型字节,返回消息* 的解析方法。例如:

 消息* parse(const char * frame)
{
//这是示例代码,在现实生活中,我显然检查缓冲区
//不是NULL,大小,等等。

switch(frame [0])
{
case 0x01:
return new FooMessage();
case 0x02:
return new BarMessage();
}

//此处抛出异常,因为消息类型未知。
}

我有时需要访问子类的方法。由于我的网络消息处理必须很快,我决定避免 dynamic_cast ,我添加了一个方法到 Message 返回消息类型的类。根据这个返回值,我使用 static_cast<> 代替正确的子类型。



这主要是因为我被告知一次 dynamic_cast<> 很慢。但是,我不知道它真正做什么,它是多么缓慢,因此,我的方法可能会像慢(或更慢),但复杂得多。



你们是怎么想到这个设计的?是常见吗?它真的比使用 dynamic_cast<> 更快吗?欢迎使用 dynamic_cast<> 时发生什么情况的详细说明!



--- EDIT ---



由于有些人问为什么:



基本上,我收到一个框架,我做了两件事:


  1. 我解析消息并构建一个消息如果帧的内容有效。除了解析部分之外,还有没有逻辑。

  2. 我收到一个消息 code> switch(message-> getType()),I static_cast<>

  3. 解决方案

    dynamic_cast的实现当然会因编译器而异。

    在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:

    1. 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.
    2. I receive a Message and depending on a switch(message->getType()), I static_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&lt;&gt;快于dynamic_cast&lt;&gt;本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆