[libcxx-commits] [libcxx] [libc++][NFC] Simplify the implementation of reserve() and shrink_to_fit() (PR #113453)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 23 11:00:20 PDT 2024
================
@@ -3349,70 +3371,48 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
if (__target_capacity == capacity())
return;
- __shrink_or_extend(__target_capacity);
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void
-basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity) {
- __annotate_delete();
- size_type __cap = capacity();
- size_type __sz = size();
-
- pointer __new_data, __p;
- bool __was_long, __now_long;
if (__fits_in_sso(__target_capacity)) {
- __was_long = true;
- __now_long = false;
- __new_data = __get_short_pointer();
- __p = __get_long_pointer();
- } else {
- if (__target_capacity > __cap) {
- // Extend
- // - called from reserve should propagate the exception thrown.
- auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
- __new_data = __allocation.ptr;
- __target_capacity = __allocation.count - 1;
- } else {
- // Shrink
- // - called from shrink_to_fit should not throw.
- // - called from reserve may throw but is not required to.
+ if (!__is_long())
+ return;
+ __annotation_guard __g(*this);
+ auto __ptr = __get_long_pointer();
+ auto __size = __get_long_size();
+ auto __cap = __get_long_cap();
+ traits_type::copy(std::__to_address(__get_short_pointer()), data(), __size + 1);
+ __alloc_traits::deallocate(__alloc_, __ptr, __cap);
+ __set_short_size(__size);
----------------
ldionne wrote:
IMO it makes more sense to group `__set_short_size` and the copy together, since this is modifying the current state of the string representation. This could also be a single "operation" if this were refactored. On the other hand, reclaiming the memory of the old long string is definitely a distinct step.
https://github.com/llvm/llvm-project/pull/113453
More information about the libcxx-commits
mailing list