清除提升的更快方法::进程间::映射
Faster method of clearing a boost::interprocess::map?
我有一个应用程序,它在共享内存中使用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 的解决方案,具体取决于您的性能需求水平。
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- Growing Boost.使用单个写入器的进程间内存映射文件
- 当一个进程截断 Boost 进程间库创建的共享内存时,进程需要重新映射
- 两个进程之间的共享映射内存在编辑时未更新
- 如何将记忆映射的文件与另一个进程写入文件同步
- 促进进程间的无序映射编译
- 简单的进程加载程序内存映射
- 从函数返回 boost::进程间内存映射文件
- 进程如何访问不属于其虚拟地址空间的内存映射 IO 地址?
- 清除提升的更快方法::进程间::映射
- Linux C++ ptrace -- 映射子进程内存的所有单元
- C++:加速进程间内存映射文件错误
- 如何正确使用文件映射并将数据传递给子进程
- 提高进程间映射线程安全性
- Boost进程间映射与int和string
- 如果进程崩溃,映射文件会发生什么
- Boost::range用于处理容器的内容,并将映射中的值管道到下一个进程
- 如何在linux中传递两个进程之间的映射
- 使用进程间映射的Boost模板错误
- 内存模块是否映射到进程的虚拟空间?