简体   繁体   English

优化C ++代码

[英]Optimizing C++ code

I'm designing classes for my application (network tool). 我正在为我的应用程序(网络工具)设计类。 I this base class: 我这个基类:

class Descriptor
{
    // ...

    public:
        virtual void data_read (void);
        virtual void data_write (void);
        virtual void data_error (void);

     protected:   
        int socket_descriptor;
    // ...
}

class TcpClient :
    public Descriptor
{
    // ...
}

Many classes are based on Descriptor. 许多类都基于描述符。 I monitor sockets' events using epoll. 我使用epoll监视套接字的事件。 When I want to look for events on TcpClient object I add the object's socket and pointer to this object to epoll, code: 当我想在TcpClient对象上查找事件时,将对象的套接字和指向该对象的指针添加到epoll,代码:

epoll_event descriptor_events;

descriptor_events.events = EPOLLIN;
descriptor_events.data.fd = this->socket_descriptor;
descriptor_events.data.ptr = this;

epoll_ctl (epoll_descriptor, EPOLL_CTL_ADD, this->socket_descriptor, &descriptor_events);

I dispatch epoll events in separate thread in this way: 我以这种方式在单独的线程中调度epoll事件:

Descriptor *descriptor (NULL);

// ...

return_value = epoll_wait (epoll_descriptor, events, 64, -1);

while (i < return_value)
{
    descriptor = (Descriptor *) (events [i]).data.ptr;

    if ((events [i]).events & EPOLLOUT)
        descriptor->data_write ();
    if ((events [i]).events & EPOLLIN)
        descriptor->data_read ();
    if ((events [i]).events & EPOLLERR)
        descriptor->data_error ();

    i++;
}

Program is going to handle a lot of data in the epoll thread, so it mean, that virtual functions will be called many times there. 程序将在epoll线程中处理大量数据,因此,这意味着虚拟函数将在其中多次调用。 I'm wondering about runtime cost of this solution. 我想知道这种解决方案的运行时成本。

I'm also thinking about two other implementations (however I'm not sure if the're much faster): 我还在考虑其他两个实现(但是我不确定是否快得多):

typedef void (*function) (Descriptor *) EventFunction;

class Descriptor
{
    // ...

    public:
        EventFunction data_read;
        EventFunction data_write;
        EventFunction data_error;

     protected:   
        Descriptor (EventFunction data_read,
                    EventFunction data_write,
                    EventFunction data_error);

        int socket_descriptor;
    // ...
}

or use CRTP. 或使用CRTP。

Maybe you have other idea of implementing this? 也许您还有其他实现此想法?

Unless proven otherwise, your original design looks fine to me. 除非另外证明,否则您的原始设计对我来说还不错。

The first rule of optimization is to measure first, then fix only hotspots that really exist. 优化的首要规则是先进行测量,然后仅修复真正存在的热点。 You'll be surprised where your code spends its time. 您会在代码花费时间的地方感到惊讶。 Dwelling on the distinction between virtual functions and function pointers is almost certainly premature optimization. 几乎可以肯定,过早地优化虚拟函数和函数指针之间的区别。 In both cases the compiler will generate code to jump to a function pointer, though with virtual functions the compiler will have to look up the vtable first. 在这两种情况下,编译器都将生成代码以跳转到函数指针,尽管使用虚函数,编译器将必须先查找vtable。 Write idiomatic C++ code to do what you want, then profile it if you have performance problems. 编写惯用的C ++代码以执行所需的操作,然后在遇到性能问题时对其进行概要分析。

(I do have one comment about your class Descriptor : Unless you're planning on having generic data_read(), data_write(), and data_error() methods I'd recommend making them pure virtual methods.) (我对您的类Descriptor有一个评论:除非您打算使用通用的data_read(),data_write()和data_error()方法,否则建议将它们设为纯虚拟方法。)

Honestly, your best bet for optimizing this code is probably to completely replace it with Boost ASIO . 老实说,优化此代码的最佳选择可能是将其完全替换为Boost ASIO As described, you're essentially re-implementing the heavily scrutinized and well tested ASIO library. 如前所述,您实际上是在重新实现经过严格审查和测试的ASIO库。 Unless you're absolutely certain you must roll your own I/O library, you'll probably save yourself a tremendous amount of development & optimization time by just using an existing solution. 除非您绝对确定必须滚动自己的I / O库,否则仅使用现有解决方案就可以节省大量的开发和优化时间。

不要重新发明车轮伞的情况下,建议您看一下Boost.Asio,因为它提供了示例代码中描述的大多数功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM