[libcxx-commits] [PATCH] D72160: Optimize / partially inline basic_string copy constructor

Martijn Vels via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 17 11:49:23 PST 2020


mvels updated this revision to Diff 238841.
mvels added a comment.

- Optimize / partially inline basic_string assignment operator

Changed init_long to accept [s,n] following discussion with EricWF


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72160/new/

https://reviews.llvm.org/D72160

Files:
  libcxx/include/string


Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -798,6 +798,10 @@
         _NOEXCEPT;
 #endif
 
+    // Optimization opportunity: do not externally instantiate the copy
+    // constructor, which inlines short string initialization. Long string
+    // initialization is delegated to the (external) __init_long()method,
+    // which results in a 3X-4X speed up for SSO initialization.
     basic_string(const basic_string& __str);
     basic_string(const basic_string& __str, const allocator_type& __a);
 
@@ -1549,6 +1553,8 @@
     inline
     void __init(size_type __n, value_type __c);
 
+    void __init_long(const value_type* __s, size_type __sz);
+
     template <class _InputIterator>
     inline
     _EnableIf
@@ -1797,6 +1803,18 @@
     traits_type::assign(__p[__sz], value_type());
 }
 
+template <class _CharT, class _Traits, class _Allocator>
+void basic_string<_CharT, _Traits, _Allocator>::__init_long(
+    const _CharT* __s, size_type __sz) {
+  size_type __cap = __recommend(__sz);
+  pointer __p = __alloc_traits::allocate(__alloc(), __cap + 1);
+  __set_long_pointer(__p);
+  __set_long_cap(__cap + 1);
+  __set_long_size(__sz);
+  traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
+  traits_type::assign(__p[__sz], value_type());
+}
+
 template <class _CharT, class _Traits, class _Allocator>
 template <class>
 basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
@@ -1840,7 +1858,7 @@
     if (!__str.__is_long())
         __r_.first().__r = __str.__r_.first().__r;
     else
-        __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
+        __init_long(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
 #if _LIBCPP_DEBUG_LEVEL >= 2
     __get_db()->__insert_c(this);
 #endif
@@ -1854,7 +1872,7 @@
     if (!__str.__is_long())
         __r_.first().__r = __str.__r_.first().__r;
     else
-        __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
+        __init_long(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
 #if _LIBCPP_DEBUG_LEVEL >= 2
     __get_db()->__insert_c(this);
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72160.238841.patch
Type: text/x-patch
Size: 2288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200117/2e1e3190/attachment.bin>


More information about the libcxx-commits mailing list