This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Optimize truncating a basic_string


Currently str.erase(n) doesn't get inlined at -O2 and calls
_M_erase(n, length() - pos), which isn't very efficient. By checking
for the common case of erasing everything up to the end of the string
and avoiding _M_erase we can get it inlined. We can do the same thing
in erase(const_iterator, const_iterator) too by checking if the second
iterator is end(), and for resize() we don't even need to check
because (when shrinking the string) we know we're erasing to the end.

Thanks to Pedro Alves for pointing out this could be improved.

	* include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI]
	(basic_string::erase(size_type, size_type)): Add fast path for
	truncating the string, by calling _M_set_length directly.
	(basic_string::erase(__const_iterator, __const_iterator)): Likewise.
	* include/bits/basic_string.tcc [_GLIBCXX_USE_CXX11_ABI]
	(basic_string::resize(size_type, _CharT)): Likewise.

Tested x86_64-linux, committed to trunk.


Attachment: patch.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]