【发布时间】:2011-09-19 23:40:33
【问题描述】:
我正在制作一个程序(想想:Launchy 之类的东西),它或多或少会遍历一堆字符串并根据某些标准对它们进行排序。
我将结果存储在vector<SearchSuggestion> 中,其中结构当前定义如下:
struct SearchSuggestion
{
std::string path;
int tag;
};
在我的程序中,我复制了很多结构(以及字符串),因为需要操作大量的文件路径等等。
虽然这会导致发布模式出现明显但很小的延迟,但它极大地减慢了我的程序的调试速度(即,击键之间有几秒钟的暂停)。寻找原因,我发现几乎所有时间都花在了以下堆栈跟踪上:
ntdll.dll!RtlCompareMemoryUlong()
ntdll.dll!RtlpAllocateHeap()
ntdll.dll!RtlAllocateHeap()
ntdll.dll!RtlDebugAllocateHeap()
ntdll.dll!string "Enabling heap debug options\n"()
ntdll.dll!RtlAllocateHeap()
msvcr90d.dll!_heap_alloc_base(unsigned __int64) C
msvcr90d.dll!_heap_alloc_dbg_impl(unsigned __int64, int, const char *, int, int *)
msvcr90d.dll!_nh_malloc_dbg_impl(unsigned __int64, int, int, const char *, int, int *)
msvcr90d.dll!_nh_malloc_dbg(unsigned __int64, int, int, const char *, int)
msvcr90d.dll!malloc(unsigned __int64)
msvcr90d.dll!operator new(unsigned __int64)
MyProgram.exe!std::_Allocate<wchar_t>(unsigned __int64, wchar_t *)
MyProgram.exe!std::allocator<wchar_t>::allocate(unsigned __int64)
MyProgram.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::_Copy(unsigned __int64, unsigned __int64)
MyProgram.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::_Grow(unsigned __int64, bool)
MyProgram.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::assign(const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > &, unsigned __int64, unsigned __int64)
MyProgram.exe!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >(const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > &)
MyProgram.exe!SearchSuggestion::SearchSuggestion(const SearchSuggestion &)
MyProgram.exe!std::_Construct<SearchSuggestion,SearchSuggestion>(SearchSuggestion *, const SearchSuggestion &)
MyProgram.exe!std::allocator<SearchSuggestion>::construct(SearchSuggestion *, const SearchSuggestion &)
MyProgram.exe!std::_Uninit_copy<SearchSuggestion * __ptr64,SearchSuggestion * __ptr64,std::allocator<SearchSuggestion> >(SearchSuggestion *, SearchSuggestion *, SearchSuggestion *, std::allocator<SearchSuggestion> &, std::_Nonscalar_ptr_iterator_tag, std::_Nonscalar_ptr_iterator_tag)
MyProgram.exe!stdext::unchecked_uninitialized_copy<SearchSuggestion * __ptr64,SearchSuggestion * __ptr64,std::allocator<SearchSuggestion> >(SearchSuggestion *, SearchSuggestion *, SearchSuggestion *, std::allocator<SearchSuggestion> &)
MyProgram.exe!std::_Uninit_move<SearchSuggestion * __ptr64,SearchSuggestion * __ptr64,std::allocator<SearchSuggestion>,std::_Undefined_move_tag>(SearchSuggestion *, SearchSuggestion *, SearchSuggestion *, std::allocator<SearchSuggestion> &, std::_Undefined_move_tag, std::_Undefined_move_tag)
MyProgram.exe!stdext::_Unchecked_uninitialized_move<SearchSuggestion * __ptr64,SearchSuggestion * __ptr64,std::allocator<SearchSuggestion> >(SearchSuggestion *, SearchSuggestion *, SearchSuggestion *, std::allocator<SearchSuggestion> &)
MyProgram.exe!std::vector<SearchSuggestion,std::allocator<SearchSuggestion> >::_Umove<SearchSuggestion * __ptr64>(SearchSuggestion *, SearchSuggestion *, SearchSuggestion *)
MyProgram.exe!std::vector<SearchSuggestion,std::allocator<SearchSuggestion> >::_Insert_n(std::_Vector_const_iterator<SearchSuggestion,std::allocator<SearchSuggestion> > *, unsigned __int64, const SearchSuggestion &)
MyProgram.exe!std::vector<SearchSuggestion,std::allocator<SearchSuggestion> >::insert(std::_Vector_const_iterator<SearchSuggestion,std::allocator<SearchSuggestion> > *, const SearchSuggestion &)
MyProgram.exe!std::vector<SearchSuggestion,std::allocator<SearchSuggestion> >::push_back(const SearchSuggestion &)
MyProgram.exe!Appender<std::vector<SearchSuggestion,std::allocator<SearchSuggestion> > >(const wchar_t *, _tfinddata *, void *)
MyProgram.exe!EnumMatches(const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > &, const std::vector<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >,std::allocator<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > > > &, int (const wchar_t *, _tfinddata *, void *)*, void *, int)
...
所以很明显,复制std::string 花费的时间太长,可能是由于引用的局部性差。
所以现在我的问题很简单:
如何提高分配大量小字符串的性能?
【问题讨论】:
-
试试 VS2010。通过移动语义和小字符串优化,我很想相信它会表现得更好。
-
@KerrekSB:整个 IDE 在 2010 年的运行速度(加起来)比我在程序中遇到的还要糟糕,所以这对这种情况没有帮助。 :(
-
EnumMatches()和Appender()到底在做什么?知道这一点可能会更容易提出避免复制字符串的建议(例如,将副本缓存在某处是可能的并且适当的)。 -
@MichaelBurr:
Appender只有 4 行——它从给定的字符串创建一个SearchSuggestion并将结果推送到vector。然而,EnumMatches是程序的主要部分——它枚举了一堆匹配项(根据给定的参数确定)并为每个匹配项调用给定的回调。他们都做了一些字符串复制。还有一个没有显示的RankMatches——它也进行字符串复制,并返回一个包含一些其他排名数据的建议对的向量。我确实可能可以缓存字符串,但这会使程序的模块化程度降低。
标签: c++ string visual-studio-2008 visual-c++ stl