[libcxx-commits] [PATCH] D102727: Add a compiler option to make string resize/__resize_default_init use precise growth instead of amortized growth.

Evan Brown via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 18 14:37:45 PDT 2021


ezbr created this revision.
ezbr added a reviewer: EricWF.
ezbr requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

In Google, we'd like to be able to test using precise growth rather than amortized growth in string resize methods in order to save memory. We intend to test internally and, assuming the effects are favorable, hopefully enable this behavior by default if possible in the future.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102727

Files:
  libcxx/include/string


Index: libcxx/include/string
===================================================================
--- libcxx/include/string
+++ libcxx/include/string
@@ -3276,9 +3276,12 @@
 basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
 {
     size_type __sz = size();
-    if (__n > __sz)
+    if (__n > __sz) {
+        #ifdef _LIBCPP_STRING_PRECISE_RESIZE
+        if (__n > capacity()) __shrink_or_extend(__recommend(__n));
+        #endif
         append(__n - __sz, __c);
-    else
+    } else
         __erase_to_end(__n);
 }
 
@@ -3288,6 +3291,9 @@
 {
     size_type __sz = size();
     if (__n > __sz) {
+      #ifdef _LIBCPP_STRING_PRECISE_RESIZE
+      if (__n > capacity()) __shrink_or_extend(__recommend(__n));
+      #endif
        __append_default_init(__n - __sz);
     } else
         __erase_to_end(__n);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102727.346275.patch
Type: text/x-patch
Size: 841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210518/c352de3d/attachment.bin>


More information about the libcxx-commits mailing list