Patchwork [v3] improve operator+(basic_string&&, basic_string&&)

login
register
mail settings
Submitter Paolo Carlini
Date Dec. 19, 2010, 3:55 p.m.
Message ID <4D0E2AE0.8060203@oracle.com>
Download mbox | patch
Permalink /patch/76137/
State New
Headers show

Comments

Paolo Carlini - Dec. 19, 2010, 3:55 p.m.
Hi,

tested x86_64-linux, committed.

Paolo.

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

Patch

Index: include/ext/vstring.h
===================================================================
--- include/ext/vstring.h	(revision 168046)
+++ include/ext/vstring.h	(working copy)
@@ -2118,7 +2118,13 @@ 
     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>
Index: include/bits/basic_string.h
===================================================================
--- include/bits/basic_string.h	(revision 168046)
+++ include/bits/basic_string.h	(working copy)
@@ -2380,7 +2380,13 @@ 
     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 @@ 
 
   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>