Skip to content

Commit 92f5d8d

Browse files
authored
[libc++] Replace __resize_default_init with resize_and_overwrite (#157121)
Since `__resize_default_init` is only ever used inside the dylib we can remove the libc++-internal API and switch to the public one. This patch inlines a bunch of functions that aren't required anymore and simplifies the code that way.
1 parent a134b06 commit 92f5d8d

File tree

7 files changed

+33
-192
lines changed

7 files changed

+33
-192
lines changed

libcxx/include/string

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,13 +1312,17 @@ public:
13121312
# if _LIBCPP_STD_VER >= 23
13131313
template <class _Op>
13141314
_LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {
1315-
__resize_default_init(__n);
1316-
__erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
1315+
size_type __sz = size();
1316+
size_type __cap = capacity();
1317+
if (__n > __cap)
1318+
__grow_by_without_replace(__cap, __n - __cap, __sz, __sz, 0);
1319+
__annotate_delete();
1320+
__set_size(__n);
1321+
__annotate_new(__n);
1322+
__erase_to_end(std::move(__op)(data(), auto(__n)));
13171323
}
13181324
# endif
13191325

1320-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
1321-
13221326
# if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE)
13231327
_LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
13241328
# endif
@@ -1410,8 +1414,6 @@ public:
14101414
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
14111415
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
14121416

1413-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __append_default_init(size_type __n);
1414-
14151417
template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
14161418
_LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
14171419
append(_InputIterator __first, _InputIterator __last) {
@@ -3081,22 +3083,6 @@ basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
30813083
return *this;
30823084
}
30833085

3084-
template <class _CharT, class _Traits, class _Allocator>
3085-
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
3086-
basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n) {
3087-
if (__n == 0)
3088-
return;
3089-
size_type __cap = capacity();
3090-
size_type __sz = size();
3091-
if (__cap - __sz < __n)
3092-
__grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
3093-
__annotate_increase(__n);
3094-
pointer __p = __get_pointer();
3095-
__sz += __n;
3096-
__set_size(__sz);
3097-
traits_type::assign(__p[__sz], value_type());
3098-
}
3099-
31003086
template <class _CharT, class _Traits, class _Allocator>
31013087
_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {
31023088
bool __is_short = !__is_long();
@@ -3435,16 +3421,6 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
34353421
__erase_to_end(__n);
34363422
}
34373423

3438-
template <class _CharT, class _Traits, class _Allocator>
3439-
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
3440-
basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n) {
3441-
size_type __sz = size();
3442-
if (__n > __sz) {
3443-
__append_default_init(__n - __sz);
3444-
} else
3445-
__erase_to_end(__n);
3446-
}
3447-
34483424
template <class _CharT, class _Traits, class _Allocator>
34493425
_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {
34503426
if (__requested_capacity > max_size())

libcxx/src/filesystem/format_string.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,19 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 0) string vformat_string(const ch
3434

3535
va_list apcopy;
3636
va_copy(apcopy, ap);
37-
int ret = ::vsnprintf(buf.data(), buf.size(), msg, apcopy);
37+
int size = ::vsnprintf(buf.data(), buf.size(), msg, apcopy);
3838
va_end(apcopy);
3939

4040
string result;
41-
if (static_cast<size_t>(ret) < buf.size()) {
42-
result.assign(buf.data(), static_cast<size_t>(ret));
41+
if (static_cast<size_t>(size) < buf.size()) {
42+
result.assign(buf.data(), static_cast<size_t>(size));
4343
} else {
4444
// we did not provide a long enough buffer on our first attempt. The
4545
// return value is the number of bytes (excluding the null byte) that are
4646
// needed for formatting.
47-
size_t size_with_null = static_cast<size_t>(ret) + 1;
48-
result.__resize_default_init(size_with_null - 1);
49-
ret = ::vsnprintf(&result[0], size_with_null, msg, ap);
50-
_LIBCPP_ASSERT_INTERNAL(static_cast<size_t>(ret) == (size_with_null - 1), "TODO");
47+
result.resize_and_overwrite(size, [&](char* res, size_t n) { return ::vsnprintf(res, n, msg, ap); });
48+
_LIBCPP_ASSERT_INTERNAL(static_cast<size_t>(size) == result.size(),
49+
"vsnprintf did not result in the same number of characters as the first attempt?");
5150
}
5251
return result;
5352
}

libcxx/test/benchmarks/containers/string.bench.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1010

11+
#include <algorithm>
1112
#include <cstdint>
1213
#include <cstdlib>
1314
#include <new>
@@ -67,6 +68,21 @@ static void BM_StringCtorDefault(benchmark::State& state) {
6768
}
6869
BENCHMARK(BM_StringCtorDefault);
6970

71+
static void BM_StringResizeAndOverwrite(benchmark::State& state) {
72+
std::string str;
73+
74+
for (auto _ : state) {
75+
benchmark::DoNotOptimize(str);
76+
str.resize_and_overwrite(10, [](char* ptr, size_t n) {
77+
std::fill_n(ptr, n, 'a');
78+
return n;
79+
});
80+
benchmark::DoNotOptimize(str);
81+
str.clear();
82+
}
83+
}
84+
BENCHMARK(BM_StringResizeAndOverwrite);
85+
7086
enum class Length { Empty, Small, Large, Huge };
7187
struct AllLengths : EnumValuesAsTuple<AllLengths, Length, 4> {
7288
static constexpr const char* Names[] = {"Empty", "Small", "Large", "Huge"};

libcxx/test/libcxx-03/strings/basic.string/string.modifiers/resize_default_initialized.pass.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

libcxx/test/libcxx/input.output/filesystems/class.directory_entry/directory_entry.mods/last_write_time.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03, c++11, c++14
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1010
// UNSUPPORTED: availability-filesystem-missing
1111
// UNSUPPORTED: no-filesystem
1212
// ADDITIONAL_COMPILE_FLAGS: -I %{libcxx-dir}/src

libcxx/test/libcxx/input.output/filesystems/convert_file_time.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03, c++11, c++14
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1010
// UNSUPPORTED: availability-filesystem-missing
1111

1212
// <filesystem>

libcxx/test/libcxx/strings/basic.string/string.modifiers/resize_default_initialized.pass.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)