diff --git a/libcxx/docs/UsingLibcxx.rst b/libcxx/docs/UsingLibcxx.rst --- a/libcxx/docs/UsingLibcxx.rst +++ b/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**: diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/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);