[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
Wed Jun 16 14:01:46 PDT 2021


ezbr updated this revision to Diff 352550.
ezbr added a comment.

Updated configuration macro documentation.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D102727

Files:
  libcxx/docs/UsingLibcxx.rst
  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);
Index: libcxx/docs/UsingLibcxx.rst
===================================================================
--- libcxx/docs/UsingLibcxx.rst
+++ libcxx/docs/UsingLibcxx.rst
@@ -241,6 +241,13 @@
   warning saying that `std::auto_ptr` is deprecated. If the macro is defined,
   no warning will be emitted. By default, this macro is not defined.
 
+**_LIBCPP_STRING_PRECISE_RESIZE**:
+  This macro enables using precise growth in `std::string::resize/__resize_default_init`,
+  replacing the default behavior of amortized exponential growth. This is a
+  tradeoff of saving memory at the expense of increased CPU usage if there are
+  cases of `resize` being used to grow a particular string repeatedly. Note
+  that `reserve` uses precise growth by default.
+
 C++17 Specific Configuration Macros
 -----------------------------------
 **_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES**:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102727.352550.patch
Type: text/x-patch
Size: 1711 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210616/72e38a07/attachment.bin>


More information about the libcxx-commits mailing list