[libcxx-commits] [libcxx] [libc++] Remove basic_string::__clear_and_shrink (PR #126050)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 6 03:28:05 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

`__clear_and_shrink` is only used in a single place and does more work than actually required.


---
Full diff: https://github.com/llvm/llvm-project/pull/126050.diff


2 Files Affected:

- (modified) libcxx/include/string (+5-15) 
- (removed) libcxx/test/libcxx/strings/basic.string/string.modifiers/clear_and_shrink.pass.cpp (-44) 


``````````diff
diff --git a/libcxx/include/string b/libcxx/include/string
index b7f2d1226946392..11af160c0f8ff47 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -1879,8 +1879,6 @@ public:
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
 
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
-
 private:
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
   __is_long() const _NOEXCEPT {
@@ -2189,7 +2187,11 @@ private:
       __alloc_ = __str.__alloc_;
     else {
       if (!__str.__is_long()) {
-        __clear_and_shrink();
+        if (__is_long()) {
+          __annotate_delete();
+          __alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
+          __rep_ = __rep();
+        }
         __alloc_ = __str.__alloc_;
       } else {
         __annotate_delete();
@@ -3820,18 +3822,6 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocat
   return true;
 }
 
-// __clear_and_shrink
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT {
-  clear();
-  if (__is_long()) {
-    __annotate_delete();
-    __alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
-    __rep_ = __rep();
-  }
-}
-
 // operator==
 
 template <class _CharT, class _Traits, class _Allocator>
diff --git a/libcxx/test/libcxx/strings/basic.string/string.modifiers/clear_and_shrink.pass.cpp b/libcxx/test/libcxx/strings/basic.string/string.modifiers/clear_and_shrink.pass.cpp
deleted file mode 100644
index 8f4c9102fde87d7..000000000000000
--- a/libcxx/test/libcxx/strings/basic.string/string.modifiers/clear_and_shrink.pass.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <string>
-
-// Call __clear_and_shrink() and ensure string invariants hold
-
-#include <string>
-#include <cassert>
-
-#include "test_macros.h"
-
-TEST_CONSTEXPR_CXX20 bool test() {
-  std::string l = "Long string so that allocation definitely, for sure, absolutely happens. Probably.";
-  std::string s = "short";
-
-  assert(l.__invariants());
-  assert(s.__invariants());
-
-  s.__clear_and_shrink();
-  assert(s.__invariants());
-  assert(s.size() == 0);
-
-  std::string::size_type cap = l.capacity();
-  l.__clear_and_shrink();
-  assert(l.__invariants());
-  assert(l.size() == 0);
-  assert(l.capacity() < cap);
-
-  return true;
-}
-
-int main(int, char**) {
-  test();
-#if TEST_STD_VER > 17
-  static_assert(test());
-#endif
-  return 0;
-}

``````````

</details>


https://github.com/llvm/llvm-project/pull/126050


More information about the libcxx-commits mailing list