清除提升的更快方法::进程间::映射

Faster method of clearing a boost::interprocess::map?

本文关键字:进程 映射 方法 清除      更新时间:2023-10-16

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

(顺便说一下,我也尝试过boost:interprocess:flat_map - 正如预期的那样,这有一个更快的清除时间(快 10 倍),但对于插入/删除操作来说太慢了。

注意:之前关于 StackOverflow 的问题涉及正常(即非共享)内存中较小 STL 映射的类似问题,但并没有真正解决问题。

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

  • 在 rbtree 的实现中发现了一个错误

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

来自 \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);
      }
   }

这里的注释清楚地表明,您可以从实现的清除中获得恒定的时间。 通过分页代码,我的理解是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.
//
/////////////////////////////////////////////////////////////////////////////

快速的谷歌搜索似乎有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 的解决方案,具体取决于您的性能需求水平。