【问题标题】:Why should I use a custom handler allocator (custom_alloc_handler) in boost::asio?为什么我应该在 boost::asio 中使用自定义处理程序分配器 (custom_alloc_handler)?
【发布时间】:2013-06-20 08:31:31
【问题描述】:

为什么要使用 boost::asio:http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/allocation/server.cpp 中示例的自定义处理程序分配器 (custom_alloc_handler) 这里显示 - 如何做到这一点,但不解释为什么。如果它总能带来一些优势,那么为什么不在其他示例中使用它呢? 自定义分配器不在以下示例中:聊天、网络服务器等。 http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/examples.html#boost_asio.examples.buffers

【问题讨论】:

    标签: boost boost-asio


    【解决方案1】:

    Asynchronous operations may need to allocate temporary objects. <...> All temporary objects associated with a handler will be deallocated before the upcall to the handler is performed. This allows the same memory to be reused for a subsequent asynchronous operation initiated by the handler.

    换句话说,上述技术是一种优化,允许用户最小化分配/取消分配的次数,从而提高性能并减少堆碎片。

    如果它总能带来一些优势,那为什么不在 其他例子?

    每个示例都强调Asio 功能的某些特定部分。对于现实生活中的应用程序,过早的优化通常不是一个好主意,因为它们会带来不必要的复杂性。因此,上述优化以及任何其他优化都应仅在适当的情况下使用。

    【讨论】:

    • 在我的应用程序中,每个异步处理程序设置有 2 个分配(大约 160 字节 +40 字节)。我认为它非常昂贵,但我们使用的是 tcmalloc,所以它不是真正的优化,但效果很好。
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多