basic_string.h (operator+(basic_string<>&&, basic_string<>&&)): Optimize better.
authorPaolo Carlini <paolo.carlini@oracle.com>
Sun, 19 Dec 2010 15:53:44 +0000 (15:53 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sun, 19 Dec 2010 15:53:44 +0000 (15:53 +0000)
2010-12-19  Paolo Carlini  <paolo.carlini@oracle.com>

* include/bits/basic_string.h (operator+(basic_string<>&&,
basic_string<>&&)): Optimize better.
* include/ext/vstring.h (operator+(__versa_string<>&&,
__versa_string<>&)): Likewise.

From-SVN: r168061

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/ext/vstring.h

index 19063dd7b22c53e2039a1415feb5ba6baab0cffd..b6f23e4413be6664cec76448377c3bd3d8358339 100644 (file)
@@ -1,3 +1,10 @@
+2010-12-19  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       * include/bits/basic_string.h (operator+(basic_string<>&&,
+       basic_string<>&&)): Optimize better.
+       * include/ext/vstring.h (operator+(__versa_string<>&&,
+       __versa_string<>&)): Likewise.
+
 2010-12-19  Paolo Carlini  <paolo.carlini@oracle.com>
 
        * testsuite/21_strings/basic_string/operators/char/4.cc: New.
index eb27e677e124c3fae9ff60dffcae527cfb249bab..bffa8afd4c698af91004e0ef1cf7892ab4043702 100644 (file)
@@ -2380,7 +2380,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     inline basic_string<_CharT, _Traits, _Alloc>
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
              basic_string<_CharT, _Traits, _Alloc>&& __rhs)
-    { return std::move(__lhs.append(__rhs)); }
+    {
+      const auto __size = __lhs.size() + __rhs.size();
+      const bool __cond = (__size > __lhs.capacity()
+                          && __size <= __rhs.capacity());
+      return __cond ? std::move(__rhs.insert(0, __lhs))
+                   : std::move(__lhs.append(__rhs));
+    }
 
   template<typename _CharT, typename _Traits, typename _Alloc>
     inline basic_string<_CharT, _Traits, _Alloc>
@@ -2390,8 +2396,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   template<typename _CharT, typename _Traits, typename _Alloc>
     inline basic_string<_CharT, _Traits, _Alloc>
-    operator+(_CharT __lhs, basic_string<_CharT,
-             _Traits, _Alloc>&& __rhs)
+    operator+(_CharT __lhs,
+             basic_string<_CharT, _Traits, _Alloc>&& __rhs)
     { return std::move(__rhs.insert(0, 1, __lhs)); }
 
   template<typename _CharT, typename _Traits, typename _Alloc>
index faca91be5098fa790c04d90b2ed64a79a321ec98..3700f3ee587d278e1919e1b5b05e5e08ae76d851 100644 (file)
@@ -2118,7 +2118,13 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     inline __versa_string<_CharT, _Traits, _Alloc, _Base>
     operator+(__versa_string<_CharT, _Traits, _Alloc, _Base>&& __lhs,
              __versa_string<_CharT, _Traits, _Alloc, _Base>&& __rhs)
-    { return std::move(__lhs.append(__rhs)); }
+    {
+      const auto __size = __lhs.size() + __rhs.size();
+      const bool __cond = (__size > __lhs.capacity()
+                          && __size <= __rhs.capacity());
+      return __cond ? std::move(__rhs.insert(0, __lhs))
+                   : std::move(__lhs.append(__rhs));
+    }
 
   template<typename _CharT, typename _Traits, typename _Alloc,
           template <typename, typename, typename> class _Base>