Optimize truncating a basic_string
authorJonathan Wakely <jwakely@redhat.com>
Fri, 23 Sep 2016 17:25:23 +0000 (18:25 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 23 Sep 2016 17:25:23 +0000 (18:25 +0100)
* 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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.tcc

index 69338c13aceeb31fbe52e98ac5ca1898011a5722..80bc018c9bba4ec6f270aee2d4c0707b0d74b020 100644 (file)
@@ -1,3 +1,12 @@
+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>
 
        * configure.ac: Define HAVE_MEMALIGN for newlib.
index e823f132d4eecb5cbfcceb7b3f7249fca10cde6c..2708cbc60b79b70a496dce594505c1eb3a6ce8f1 100644 (file)
@@ -1709,8 +1709,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       basic_string&
       erase(size_type __pos = 0, size_type __n = npos)
       {
-       this->_M_erase(_M_check(__pos, "basic_string::erase"),
-                      _M_limit(__pos, __n));
+       _M_check(__pos, "basic_string::erase");
+       if (__n == npos)
+         this->_M_set_length(__pos);
+       else if (__n != 0)
+         this->_M_erase(__pos, _M_limit(__pos, __n));
        return *this;
       }
 
@@ -1747,7 +1750,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
                                 && __last <= end());
         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);
       }
 
index 0080d2b0e2f95bc62b7c34d2c9f0aff1d1952e8d..df1e8ddec75f730fdb20830ff93460d154e4bd79 100644 (file)
@@ -351,7 +351,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       if (__size < __n)
        this->append(__n - __size, __c);
       else if (__n < __size)
-       this->_M_erase(__n, __size - __n);
+       this->_M_set_length(__n);
     }
 
   template<typename _CharT, typename _Traits, typename _Alloc>