Skip to content

Commit 2c43f3c

Browse files
committed
[libc++] Refactor how we do amortized growth
1 parent 5e3ffd6 commit 2c43f3c

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

libcxx/include/string

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,25 @@ private:
22852285
return __long(__buffer, __capacity);
22862286
}
22872287

2288+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
2289+
__allocate_long_buffer_for_amortized_growth(size_type __required_size) {
2290+
auto __new_cap = [&] {
2291+
size_type __max_size = max_size();
2292+
if (__required_size > __max_size)
2293+
__throw_length_error();
2294+
size_type __current_cap = capacity();
2295+
_LIBCPP_ASSERT_INTERNAL(
2296+
__current_cap < __required_size, "Trying to grow string even though there is enough capacity already?");
2297+
if (__current_cap > __max_size / 2 - __alignment)
2298+
return __max_size;
2299+
return std::max(__required_size, 2 * __current_cap);
2300+
}();
2301+
2302+
__long __buffer = __allocate_long_buffer(__alloc_, __new_cap);
2303+
__buffer.__size_ = __required_size;
2304+
return __buffer;
2305+
}
2306+
22882307
// Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
22892308
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
22902309
__annotate_delete();
@@ -2384,19 +2403,6 @@ private:
23842403
return __guess;
23852404
}
23862405

2387-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
2388-
__get_amortized_growth_capacity(size_type __required_capacity) {
2389-
size_type __max_size = max_size();
2390-
if (__required_capacity > __max_size)
2391-
__throw_length_error();
2392-
size_type __current_cap = capacity();
2393-
_LIBCPP_ASSERT_INTERNAL(
2394-
__current_cap < __required_capacity, "Trying to grow string even though there is enough capacity already?");
2395-
if (__current_cap > __max_size / 2 - __alignment)
2396-
return __max_size;
2397-
return std::max(__required_capacity, 2 * __current_cap);
2398-
}
2399-
24002406
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);
24012407
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);
24022408

@@ -2727,7 +2733,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27272733
size_type __n_del,
27282734
size_type __n_add,
27292735
const value_type* __p_new_stuff) {
2730-
__long __buffer = __allocate_long_buffer(__alloc_, __get_amortized_growth_capacity(__old_cap + __delta_cap));
2736+
__long __buffer = __allocate_long_buffer_for_amortized_growth(__old_cap + __delta_cap);
27312737
pointer __old_p = __get_pointer();
27322738
__annotate_delete();
27332739
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
@@ -2760,7 +2766,7 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27602766
size_type __n_copy,
27612767
size_type __n_del,
27622768
size_type __n_add) {
2763-
__long __buffer = __allocate_long_buffer(__alloc_, __get_amortized_growth_capacity(__old_cap + __delta_cap));
2769+
__long __buffer = __allocate_long_buffer_for_amortized_growth(__old_cap + __delta_cap);
27642770
pointer __old_p = __get_pointer();
27652771
if (__n_copy != 0)
27662772
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);

0 commit comments

Comments
 (0)