更快速清除boost :: interprocess :: map的方法?

Pau*_*l R 17 c++ optimization boost stl map

我有一个使用boost::interprocess::map共享内存的应用程序.地图包含大量元素(100k到10M),一切都运行良好,但有一个例外:地图必须定期清除,这似乎需要每个元素大约4μs(所以40秒最坏的情况),这是不可接受的申请.它看起来clear()实际上是单独删除每个地图元素并在每次删除后重新平衡树,所以当你有大量的元素时,这是非常低效的.理想情况下clear(),只需删除所有元素而不进行任何重新平衡 - 我有什么方法可以clear()自己实现这种优化方法?

(顺便说一句,我也尝试过boost:interprocess:flat_map- 这可以预期更快的时间(快10倍),但是插入/删除操作的速度太慢了.)

注:在StackOverflow上较早的问题触及了一个类似的问题较小STL的地图在正常(即不共享)的内存,但并没有真正解决问题.

Pat*_*ckV 2

通过查看 Boost 代码,我猜您有:

  • 发现rbtree实现中的一个bug

或者

  • 使用安全模式或自动取消链接进行编码

来自 \boost_1_54_0\boost\intrusive\rbtree.hpp

   //! <b>Effects</b>: Erases all of the elements.
   //!
   //! <b>Complexity</b>: Linear to the number of elements on the container.
   //!   if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
   //!
   //! <b>Throws</b>: Nothing.
   //!
   //! <b>Note</b>: Invalidates the iterators (but not the references)
   //!    to the erased elements. No destructors are called.
   void clear()
   {
      if(safemode_or_autounlink){
         this->clear_and_dispose(detail::null_disposer());
      }
      else{
         node_algorithms::init_header(this->priv_header_ptr());
         this->priv_size_traits().set_size(0);
      }
   }
Run Code Online (Sandbox Code Playgroud)

这里的评论清楚地表明,您可以从实施的清除中获得恒定的时间。翻阅 boost 代码,我的解读是 interprocess::map 最终使用此 rbtree 作为底层数据结构。我没有注意到任何地方设置了安全模式或自动取消链接,但我可能会错过它。如果您设置了其中一个,我会首先看看没有它我是否可以生存,如果是的话,您的性能问题有望消失。

如果这是 boost 中的错误,您必须解决它。我会立即使用标题中的联系信息进行报告:

/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga  2006-2012
//
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/intrusive for documentation.
//
/////////////////////////////////////////////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)

谷歌快速搜索似乎有 Ion 的联系方式:

http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=user_nodes&user=161440

或者访问http://www.boost.org/development/bugs.html

然后根据您的性能需求级别实施 Paul R 或 rileyberton 的解决方案。