【发布时间】:2015-02-24 15:24:46
【问题描述】:
我有如下工作的 io_service 后调用:
_io_service.post(std::tr1::bind(&BlitzLogger::push,this,
std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
typename boost::decay<Args const &>::type ...>, this,
t, args ...)));
我怀疑绑定调用有一些可以消除的开销,所以我开始为处理程序布置自定义分配器,如
所述 http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/example/allocation/server.cpp
接下来,我想做如下事情:
_io_service.post(
makeCustomAllocHandler(_allocator1,std::tr1::bind(&BlitzLogger::push,this,
makeCustomAllocHandler(_allocator2,std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
typename boost::decay<Args const &>::type ...>,this,t,args ...)))));
上面这段代码抛出编译时错误(模板参数扣除/替换失败的主机), 但是,如果我删除 _io_service.post 调用,并将其限制为
makeCustomAllocHandler(_allocator1,std::tr1::bind(&BlitzLogger::push,this,
makeCustomAllocHandler(_allocator2,std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
typename boost::decay<Args const &>::type ...>,this,t,args ...))));
然后代码编译得很好。
所以很明显问题是 makeCustomAllocHandler 的返回类型不符合 post 函数的模板处理程序参数。
为什么会发生这种情况,我该如何解决这个问题。
【问题讨论】:
-
考虑使用分析器来识别瓶颈。自定义分配器处理程序不会消除调用
std::bind()或其返回的函子的开销,因为asio_handler_allocate()挂钩仅用于控制临时处理程序的分配策略。为了最小化函子调用开销,需要编写特定的函子而不是包装std::bind函子。 -
@TannerSansbury 是的,性能数字似乎暗示了你在说什么
标签: c++ templates boost bind boost-asio