Skip to content

Commit f9401c7

Browse files
philnik777copybara-github
authored andcommitted
[libc++][NFC] Refactor __libcpp_datasizeof to be a variable template (#87769)
This decreases memory consumption and compiles times slightly and removes a bit of boilderplate. NOKEYCHECK=True GitOrigin-RevId: d30f6bc5cd9579204864c944f127011be70b2c74
1 parent 65c9bc6 commit f9401c7

File tree

6 files changed

+52
-53
lines changed

6 files changed

+52
-53
lines changed

include/__string/constexpr_c_functions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ __constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
224224
std::__assign_trivially_copyable(__dest[__i], __src[__i]);
225225
}
226226
} else if (__count > 0) {
227-
::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __libcpp_datasizeof<_Tp>::value);
227+
::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);
228228
}
229229
return __dest;
230230
}

include/__type_traits/datasizeof.h

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,38 @@
2626

2727
_LIBCPP_BEGIN_NAMESPACE_STD
2828

29-
template <class _Tp>
30-
struct __libcpp_datasizeof {
3129
#if __has_extension(datasizeof)
32-
static const size_t value = __datasizeof(_Tp);
30+
template <class _Tp>
31+
inline const size_t __datasizeof_v = __datasizeof(_Tp);
3332
#else
3433
// NOLINTNEXTLINE(readability-redundant-preprocessor) This is https://llvm.org/PR64825
3534
# if __has_cpp_attribute(__no_unique_address__)
36-
template <class = char>
37-
struct _FirstPaddingByte {
38-
[[__no_unique_address__]] _Tp __v_;
39-
char __first_padding_byte_;
40-
};
35+
template <class _Tp>
36+
struct _FirstPaddingByte {
37+
[[__no_unique_address__]] _Tp __v_;
38+
char __first_padding_byte_;
39+
};
4140
# else
42-
template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
43-
struct _FirstPaddingByte : _Tp {
44-
char __first_padding_byte_;
45-
};
41+
template <class _Tp, bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
42+
struct _FirstPaddingByte : _Tp {
43+
char __first_padding_byte_;
44+
};
4645

47-
template <>
48-
struct _FirstPaddingByte<true> {
49-
_Tp __v_;
50-
char __first_padding_byte_;
51-
};
46+
template <class _Tp>
47+
struct _FirstPaddingByte<_Tp, true> {
48+
_Tp __v_;
49+
char __first_padding_byte_;
50+
};
5251
# endif // __has_cpp_attribute(__no_unique_address__)
5352

54-
// _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
55-
// the use as an extension.
56-
_LIBCPP_DIAGNOSTIC_PUSH
57-
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
58-
static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
59-
_LIBCPP_DIAGNOSTIC_POP
53+
// _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
54+
// the use as an extension.
55+
_LIBCPP_DIAGNOSTIC_PUSH
56+
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
57+
template <class _Tp>
58+
inline const size_t __datasizeof_v = offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);
59+
_LIBCPP_DIAGNOSTIC_POP
6060
#endif // __has_extension(datasizeof)
61-
};
6261

6362
_LIBCPP_END_NAMESPACE_STD
6463

test/libcxx/type_traits/datasizeof.compile.pass.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#include <__type_traits/datasizeof.h>
1010
#include <cstdint>
1111

12-
static_assert(std::__libcpp_datasizeof<std::int8_t>::value == 1, "");
13-
static_assert(std::__libcpp_datasizeof<std::int16_t>::value == 2, "");
14-
static_assert(std::__libcpp_datasizeof<std::int32_t>::value == 4, "");
15-
static_assert(std::__libcpp_datasizeof<std::int64_t>::value == 8, "");
12+
static_assert(std::__datasizeof_v<std::int8_t> == 1, "");
13+
static_assert(std::__datasizeof_v<std::int16_t> == 2, "");
14+
static_assert(std::__datasizeof_v<std::int32_t> == 4, "");
15+
static_assert(std::__datasizeof_v<std::int64_t> == 8, "");
1616

1717
struct OneBytePadding {
1818
OneBytePadding() {}
@@ -22,9 +22,9 @@ struct OneBytePadding {
2222
};
2323

2424
#if defined(_WIN32) && !defined(__MINGW32__)
25-
static_assert(std::__libcpp_datasizeof<OneBytePadding>::value == 4, "");
25+
static_assert(std::__datasizeof_v<OneBytePadding> == 4, "");
2626
#else
27-
static_assert(std::__libcpp_datasizeof<OneBytePadding>::value == 3, "");
27+
static_assert(std::__datasizeof_v<OneBytePadding> == 3, "");
2828
#endif
2929

3030
struct InBetweenPadding {
@@ -35,4 +35,4 @@ struct InBetweenPadding {
3535
std::int16_t c;
3636
};
3737

38-
static_assert(std::__libcpp_datasizeof<InBetweenPadding>::value == 8, "");
38+
static_assert(std::__datasizeof_v<InBetweenPadding> == 8, "");

test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ static_assert(sizeof(std::expected<B, B>) == sizeof(B));
4747

4848
// Check that `expected`'s datasize is large enough for the parameter type(s).
4949
static_assert(sizeof(std::expected<BoolWithPadding, Empty>) ==
50-
std::__libcpp_datasizeof<std::expected<BoolWithPadding, Empty>>::value);
50+
std::__datasizeof_v<std::expected<BoolWithPadding, Empty>>);
5151
static_assert(sizeof(std::expected<Empty, BoolWithPadding>) ==
52-
std::__libcpp_datasizeof<std::expected<Empty, BoolWithPadding>>::value);
52+
std::__datasizeof_v<std::expected<Empty, BoolWithPadding>>);
5353

5454
// In this case, there should be tail padding in the `expected` because `A`
5555
// itself does _not_ have tail padding.
56-
static_assert(sizeof(std::expected<A, A>) > std::__libcpp_datasizeof<std::expected<A, A>>::value);
56+
static_assert(sizeof(std::expected<A, A>) > std::__datasizeof_v<std::expected<A, A>>);
5757

5858
// Test with some real types.
5959
static_assert(sizeof(std::expected<std::optional<int>, int>) == 8);
60-
static_assert(std::__libcpp_datasizeof<std::expected<std::optional<int>, int>>::value == 8);
60+
static_assert(std::__datasizeof_v<std::expected<std::optional<int>, int>> == 8);
6161

6262
static_assert(sizeof(std::expected<int, std::optional<int>>) == 8);
63-
static_assert(std::__libcpp_datasizeof<std::expected<int, std::optional<int>>>::value == 8);
63+
static_assert(std::__datasizeof_v<std::expected<int, std::optional<int>>> == 8);
6464

6565
static_assert(sizeof(std::expected<int, int>) == 8);
66-
static_assert(std::__libcpp_datasizeof<std::expected<int, int>>::value == 5);
66+
static_assert(std::__datasizeof_v<std::expected<int, int>> == 5);
6767

6868
// clang-format off
69-
static_assert(std::__libcpp_datasizeof<int>::value == 4);
70-
static_assert(std::__libcpp_datasizeof<std::expected<int, int>>::value == 5);
71-
static_assert(std::__libcpp_datasizeof<std::expected<std::expected<int, int>, int>>::value == 8);
72-
static_assert(std::__libcpp_datasizeof<std::expected<std::expected<std::expected<int, int>, int>, int>>::value == 9);
73-
static_assert(std::__libcpp_datasizeof<std::expected<std::expected<std::expected<std::expected<int, int>, int>, int>, int>>::value == 12);
69+
static_assert(std::__datasizeof_v<int> == 4);
70+
static_assert(std::__datasizeof_v<std::expected<int, int>> == 5);
71+
static_assert(std::__datasizeof_v<std::expected<std::expected<int, int>, int>> == 8);
72+
static_assert(std::__datasizeof_v<std::expected<std::expected<std::expected<int, int>, int>, int>> == 9);
73+
static_assert(std::__datasizeof_v<std::expected<std::expected<std::expected<std::expected<int, int>, int>, int>, int>> == 12);
7474
// clang-format on

test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ static_assert(sizeof(std::expected<void, B>) == sizeof(B));
4545

4646
// Check that `expected`'s datasize is large enough for the parameter type(s).
4747
static_assert(sizeof(std::expected<void, BoolWithPadding>) ==
48-
std::__libcpp_datasizeof<std::expected<void, BoolWithPadding>>::value);
48+
std::__datasizeof_v<std::expected<void, BoolWithPadding>>);
4949

5050
// In this case, there should be tail padding in the `expected` because `A`
5151
// itself does _not_ have tail padding.
52-
static_assert(sizeof(std::expected<void, A>) > std::__libcpp_datasizeof<std::expected<void, A>>::value);
52+
static_assert(sizeof(std::expected<void, A>) > std::__datasizeof_v<std::expected<void, A>>);
5353

5454
// Test with some real types.
5555
static_assert(sizeof(std::expected<void, std::optional<int>>) == 8);
56-
static_assert(std::__libcpp_datasizeof<std::expected<void, std::optional<int>>>::value == 8);
56+
static_assert(std::__datasizeof_v<std::expected<void, std::optional<int>>> == 8);
5757

5858
static_assert(sizeof(std::expected<void, int>) == 8);
59-
static_assert(std::__libcpp_datasizeof<std::expected<void, int>>::value == 5);
59+
static_assert(std::__datasizeof_v<std::expected<void, int>> == 5);
6060

6161
// clang-format off
62-
static_assert(std::__libcpp_datasizeof<int>::value == 4);
63-
static_assert(std::__libcpp_datasizeof<std::expected<void, int>>::value == 5);
64-
static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, int>>>::value == 8);
65-
static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, std::expected<void, int>>>>::value == 9);
66-
static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, std::expected<void, std::expected<void, int>>>>>::value == 12);
62+
static_assert(std::__datasizeof_v<int> == 4);
63+
static_assert(std::__datasizeof_v<std::expected<void, int>> == 5);
64+
static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, int>>> == 8);
65+
static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, std::expected<void, int>>>> == 9);
66+
static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, std::expected<void, std::expected<void, int>>>>> == 12);
6767
// clang-format on

test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void test_type() {
4646
static_assert(!std::is_empty<Array>::value, "");
4747

4848
// Make sure empty arrays don't have padding bytes
49-
LIBCPP_STATIC_ASSERT(std::__libcpp_datasizeof<Array>::value == sizeof(Array), "");
49+
LIBCPP_STATIC_ASSERT(std::__datasizeof_v<Array> == sizeof(Array), "");
5050
}
5151

5252
{

0 commit comments

Comments
 (0)