【发布时间】:2016-04-12 12:48:41
【问题描述】:
在将我们的项目升级到 Visual Studio 2015 时,我们遇到了一个问题,即销毁大的 vector<unsigned char>,它正在存储一个图像 - 大约 5 Mb。在 Debug 构建中大约需要 0.5 秒,而对于 char(POD 类型),原则上应该只调用一次内存释放调用。
调试器显示 VS 2015 (VC 14.0) 中的实现会遍历被破坏数组中的每个元素并调用其析构函数 - 无论它是 POD 类型还是复杂类型。所以我理解了这个问题以及为什么它只出现在 Debug 版本中 - 在 Release 版本中,编译器足够聪明,可以删除不必要的空函数调用。
实际上,在 Visual Studio 的所有早期版本(检查 2003 和 2012)中都有标量类型的专门化:
template<class _Alloc> inline
void _Destroy_range(typename _Alloc::pointer _First,
typename _Alloc::pointer _Last, _Alloc& _Al,
_Scalar_ptr_iterator_tag)
{ // destroy [_First, _Last), scalar type (do nothing)
}
但现在它已经消失了,我们所拥有的只是
template<class _Alloc> inline
void _Destroy_range(typename _Alloc::pointer _First,
typename _Alloc::pointer _Last, _Alloc& _Al)
{ // destroy [_First, _Last)
for (; _First != _Last; ++_First)
_Al.destroy(_STD addressof(*_First));
}
我只是不明白为什么删除了这个有用且简单的优化。而且我找不到任何来自 Microsoft 的 cmets。
问题:
- 有人知道这种变化的原因吗?
- 有没有办法在不更改代码中每个 POD 向量的情况下解决缓慢的销毁问题(更改为自定义容器或
unique_ptr<type[]>/boost::scoped_array)?
【问题讨论】:
标签: vector visual-studio-2015 msvcrt