[libcxx-commits] [PATCH] D109510: [libc++] string: Allocate exactly when growing from short to long string.

Clement Courbet via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 10 07:31:25 PDT 2021


courbet updated this revision to Diff 371920.
courbet added a comment.

Address review comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D109510

Files:
  libcxx/include/string


Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -2278,8 +2278,11 @@
     if (__delta_cap > __ms - __old_cap - 1)
         this->__throw_length_error();
     pointer __old_p = __get_pointer();
+    bool __was_short = __old_cap < __min_cap;
     size_type __cap = __old_cap < __ms / 2 - __alignment ?
-                          __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
+                          __recommend(
+                              _VSTD::max(__old_cap + __delta_cap,
+                                         __was_short ? 0 : 2 * __old_cap)) :
                           __ms - 1;
     pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
     __invalidate_all_iterators();
@@ -2292,7 +2295,7 @@
     if (__sec_cp_sz != 0)
         traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
                           _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
-    if (__old_cap+1 != __min_cap)
+    if (!__was_short)
         __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
     __set_long_pointer(__p);
     __set_long_cap(__cap+1);
@@ -2310,8 +2313,11 @@
     if (__delta_cap > __ms - __old_cap)
         this->__throw_length_error();
     pointer __old_p = __get_pointer();
+    bool __was_short = __old_cap < __min_cap;
     size_type __cap = __old_cap < __ms / 2 - __alignment ?
-                          __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
+                          __recommend(
+                              _VSTD::max(__old_cap + __delta_cap,
+                                         __was_short ? 0 : 2 * __old_cap)) :
                           __ms - 1;
     pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
     __invalidate_all_iterators();
@@ -2323,7 +2329,7 @@
         traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
                           _VSTD::__to_address(__old_p) + __n_copy + __n_del,
                           __sec_cp_sz);
-    if (__old_cap+1 != __min_cap)
+    if (!__was_short)
         __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
     __set_long_pointer(__p);
     __set_long_cap(__cap+1);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109510.371920.patch
Type: text/x-patch
Size: 2289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210910/0628196d/attachment.bin>


More information about the libcxx-commits mailing list