Commit a922c5ff by Jonathan Wakely Committed by Jonathan Wakely

Optimize truncating a basic_string

	* 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.

From-SVN: r240446
parent b862552d
2016-09-23 Jonathan Wakely <jwakely@redhat.com>
* 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.
2016-09-22 Jason Merrill <jason@redhat.com> 2016-09-22 Jason Merrill <jason@redhat.com>
* configure.ac: Define HAVE_MEMALIGN for newlib. * configure.ac: Define HAVE_MEMALIGN for newlib.
......
...@@ -1709,8 +1709,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 ...@@ -1709,8 +1709,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
basic_string& basic_string&
erase(size_type __pos = 0, size_type __n = npos) erase(size_type __pos = 0, size_type __n = npos)
{ {
this->_M_erase(_M_check(__pos, "basic_string::erase"), _M_check(__pos, "basic_string::erase");
_M_limit(__pos, __n)); if (__n == npos)
this->_M_set_length(__pos);
else if (__n != 0)
this->_M_erase(__pos, _M_limit(__pos, __n));
return *this; return *this;
} }
...@@ -1747,7 +1750,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 ...@@ -1747,7 +1750,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
&& __last <= end()); && __last <= end());
const size_type __pos = __first - begin(); const size_type __pos = __first - begin();
this->_M_erase(__pos, __last - __first); if (__last == end())
this->_M_set_length(__pos);
else
this->_M_erase(__pos, __last - __first);
return iterator(this->_M_data() + __pos); return iterator(this->_M_data() + __pos);
} }
......
...@@ -351,7 +351,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -351,7 +351,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
if (__size < __n) if (__size < __n)
this->append(__n - __size, __c); this->append(__n - __size, __c);
else if (__n < __size) else if (__n < __size)
this->_M_erase(__n, __size - __n); this->_M_set_length(__n);
} }
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment