[libcxx-commits] [libcxx] [libc++] Simplify string::reserve (PR #114869)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 4 13:00:39 PST 2024


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/114869

We're checking quite a few things that are either trivially true or trivially false. These cases have been introduced when we changed `reserve()` to never shrink.


>From 1a44b94798b0701876b09a38e99bc97730fee016 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 4 Nov 2024 21:59:00 +0100
Subject: [PATCH] [libc++] Simplify string::reserve

---
 libcxx/include/string | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/string b/libcxx/include/string
index 20e44eaca2ac7a..e4b2d7a8d964c1 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2083,6 +2083,8 @@ private:
     size_type __guess          = __align_it<__boundary>(__s + 1) - 1;
     if (__guess == __min_cap)
       __guess += __endian_factor;
+
+    _LIBCPP_ASSERT_INTERNAL(__guess >= __s, "recommendation is below the requested size");
     return __guess;
   }
 
@@ -3346,12 +3348,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
   if (__requested_capacity <= capacity())
     return;
 
-  size_type __target_capacity = std::max(__requested_capacity, size());
-  __target_capacity           = __recommend(__target_capacity);
-  if (__target_capacity == capacity())
-    return;
-
-  __shrink_or_extend(__target_capacity);
+  __shrink_or_extend(__recommend(__requested_capacity));
 }
 
 template <class _CharT, class _Traits, class _Allocator>



More information about the libcxx-commits mailing list