[libcxx-commits] [libcxx] [libc++] Introduce basic_string::__recommend_growing (PR #162633)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 18 03:31:40 PST 2025


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/162633

>From 4d1a4413fec6c62b7b66b56c2bd4805f50aae0c7 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 9 Oct 2025 12:37:45 +0200
Subject: [PATCH] [libc++] Introduce basic_string::__recommend_growing

---
 libcxx/include/string | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/string b/libcxx/include/string
index c4806069d0b44..5a844a91c2fd7 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2285,6 +2285,22 @@ private:
     return __long(__buffer, __capacity);
   }
 
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
+  __allocate_long_buffer_for_growing(_Allocator& __alloc, size_type __required_capacity) {
+    auto __growth_capacity = [&] {
+      size_type __max_size = max_size();
+      if (__required_capacity > __max_size)
+        __throw_length_error();
+      size_type __current_cap = capacity();
+      _LIBCPP_ASSERT_INTERNAL(
+          __current_cap < __required_capacity, "Trying to grow string even though there is enough capacity already?");
+      if (__current_cap > __max_size / 2 - __alignment)
+        return __max_size;
+      return std::max(__required_capacity, 2 * __current_cap);
+    }();
+    return __allocate_long_buffer(__alloc, __growth_capacity);
+  }
+
   // Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
     __annotate_delete();
@@ -2714,14 +2730,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
     size_type __n_del,
     size_type __n_add,
     const value_type* __p_new_stuff) {
-  size_type __ms = max_size();
-  if (__delta_cap > __ms - __old_cap)
-    __throw_length_error();
+  __long __buffer = __allocate_long_buffer_for_growing(__alloc_, __old_cap + __delta_cap);
   pointer __old_p = __get_pointer();
-  size_type __cap = __old_cap < __ms / 2 - __alignment ? std::max(__old_cap + __delta_cap, 2 * __old_cap) : __ms;
   __annotate_delete();
-  auto __guard    = std::__make_scope_guard(__annotate_new_size(*this));
-  __long __buffer = __allocate_long_buffer(__alloc_, __cap);
+  auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
   if (__n_copy != 0)
     traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
   if (__n_add != 0)
@@ -2751,12 +2763,8 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
     size_type __n_copy,
     size_type __n_del,
     size_type __n_add) {
-  size_type __ms = max_size();
-  if (__delta_cap > __ms - __old_cap)
-    this->__throw_length_error();
+  __long __buffer = __allocate_long_buffer_for_growing(__alloc_, __old_cap + __delta_cap);
   pointer __old_p = __get_pointer();
-  size_type __cap = __old_cap < __ms / 2 - __alignment ? std::max(__old_cap + __delta_cap, 2 * __old_cap) : __ms;
-  __long __buffer = __allocate_long_buffer(__alloc_, __cap);
   if (__n_copy != 0)
     traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
   size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;



More information about the libcxx-commits mailing list