Skip to content

Commit 763312f

Browse files
committed
[libc++] Refactor basic_string::__recommend
1 parent d7eade1 commit 763312f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

libcxx/include/string

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,9 @@ private:
22462246
// Allocate a buffer of __capacity size with __alloc and return it
22472247
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
22482248
__allocate_long_buffer(_Allocator& __alloc, size_type __capacity) {
2249-
auto __buffer = std::__allocate_at_least(__alloc, __recommend(__capacity) + 1);
2249+
_LIBCPP_ASSERT_INTERNAL(
2250+
!__fits_in_sso(__capacity), "Trying to allocate long buffer which fits into the small buffer");
2251+
auto __buffer = std::__allocate_at_least(__alloc, __align_allocation_size(__capacity) + 1);
22502252

22512253
if (__libcpp_is_constant_evaluated()) {
22522254
for (size_type __i = 0; __i != __buffer.count; ++__i)
@@ -2344,16 +2346,15 @@ private:
23442346
return (__s + (__a - 1)) & ~(__a - 1);
23452347
}
23462348
enum { __alignment = 8 };
2347-
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT {
2348-
if (__s < __min_cap) {
2349-
return static_cast<size_type>(__min_cap) - 1;
2350-
}
2349+
2350+
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
2351+
__align_allocation_size(size_type __size) _NOEXCEPT {
23512352
const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : __endian_factor;
2352-
size_type __guess = __align_it<__boundary>(__s + 1) - 1;
2353+
size_type __guess = __align_it<__boundary>(__size + 1) - 1;
23532354
if (__guess == __min_cap)
23542355
__guess += __endian_factor;
23552356

2356-
_LIBCPP_ASSERT_INTERNAL(__guess >= __s, "recommendation is below the requested size");
2357+
_LIBCPP_ASSERT_INTERNAL(__guess >= __size, "aligned allocation size is below the requested size");
23572358
return __guess;
23582359
}
23592360

@@ -3410,18 +3411,15 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
34103411

34113412
template <class _CharT, class _Traits, class _Allocator>
34123413
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {
3413-
size_type __target_capacity = __recommend(size());
3414-
if (__target_capacity == capacity())
3414+
if (!__is_long())
34153415
return;
34163416

3417-
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
3418-
34193417
// We're a long string and we're shrinking into the small buffer.
34203418
const auto __ptr = __get_long_pointer();
34213419
const auto __size = __get_long_size();
34223420
const auto __cap = __get_long_cap();
34233421

3424-
if (__fits_in_sso(__target_capacity)) {
3422+
if (__fits_in_sso(__align_allocation_size(size()))) {
34253423
__annotation_guard __g(*this);
34263424
__set_short_size(__size);
34273425
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);

0 commit comments

Comments
 (0)