Skip to content

Commit c388690

Browse files
authored
[libc++][NFC] Simplify copy and move lowering to memmove a bit (#83574)
We've introduced `__constexpr_memmove` a while ago, which simplified the implementation of the copy and move lowering a bit. This allows us to remove some of the boilerplate.
1 parent 009f88f commit c388690

File tree

5 files changed

+15
-48
lines changed

5 files changed

+15
-48
lines changed

libcxx/include/__algorithm/copy.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ template <class, class _InIter, class _Sent, class _OutIter>
3232
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter> __copy(_InIter, _Sent, _OutIter);
3333

3434
template <class _AlgPolicy>
35-
struct __copy_loop {
35+
struct __copy_impl {
3636
template <class _InIter, class _Sent, class _OutIter>
3737
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
3838
operator()(_InIter __first, _Sent __last, _OutIter __result) const {
@@ -94,9 +94,7 @@ struct __copy_loop {
9494
__local_first = _Traits::__begin(++__segment_iterator);
9595
}
9696
}
97-
};
9897

99-
struct __copy_trivial {
10098
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
10199
template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
102100
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
@@ -108,7 +106,7 @@ struct __copy_trivial {
108106
template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
109107
pair<_InIter, _OutIter> inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
110108
__copy(_InIter __first, _Sent __last, _OutIter __result) {
111-
return std::__dispatch_copy_or_move<_AlgPolicy, __copy_loop<_AlgPolicy>, __copy_trivial>(
109+
return std::__copy_move_unwrap_iters<__copy_impl<_AlgPolicy> >(
112110
std::move(__first), std::move(__last), std::move(__result));
113111
}
114112

libcxx/include/__algorithm/copy_backward.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InIter, _OutIter>
3333
__copy_backward(_InIter __first, _Sent __last, _OutIter __result);
3434

3535
template <class _AlgPolicy>
36-
struct __copy_backward_loop {
36+
struct __copy_backward_impl {
3737
template <class _InIter, class _Sent, class _OutIter>
3838
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
3939
operator()(_InIter __first, _Sent __last, _OutIter __result) const {
@@ -104,9 +104,7 @@ struct __copy_backward_loop {
104104
__local_last = _Traits::__end(__segment_iterator);
105105
}
106106
}
107-
};
108107

109-
struct __copy_backward_trivial {
110108
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
111109
template <class _In, class _Out, __enable_if_t<__can_lower_copy_assignment_to_memmove<_In, _Out>::value, int> = 0>
112110
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
@@ -118,7 +116,7 @@ struct __copy_backward_trivial {
118116
template <class _AlgPolicy, class _BidirectionalIterator1, class _Sentinel, class _BidirectionalIterator2>
119117
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator1, _BidirectionalIterator2>
120118
__copy_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result) {
121-
return std::__dispatch_copy_or_move<_AlgPolicy, __copy_backward_loop<_AlgPolicy>, __copy_backward_trivial>(
119+
return std::__copy_move_unwrap_iters<__copy_backward_impl<_AlgPolicy> >(
122120
std::move(__first), std::move(__last), std::move(__result));
123121
}
124122

libcxx/include/__algorithm/copy_move_common.h

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,17 @@ __copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) {
8181

8282
// Iterator unwrapping and dispatching to the correct overload.
8383

84-
template <class _F1, class _F2>
85-
struct __overload : _F1, _F2 {
86-
using _F1::operator();
87-
using _F2::operator();
88-
};
89-
90-
template <class _InIter, class _Sent, class _OutIter, class = void>
91-
struct __can_rewrap : false_type {};
92-
93-
template <class _InIter, class _Sent, class _OutIter>
94-
struct __can_rewrap<_InIter,
95-
_Sent,
96-
_OutIter,
97-
// Note that sentinels are always copy-constructible.
98-
__enable_if_t< is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value > >
99-
: true_type {};
84+
template <class _InIter, class _OutIter>
85+
struct __can_rewrap
86+
: integral_constant<bool, is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value> {};
10087

10188
template <class _Algorithm,
10289
class _InIter,
10390
class _Sent,
10491
class _OutIter,
105-
__enable_if_t<__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0>
92+
__enable_if_t<__can_rewrap<_InIter, _OutIter>::value, int> = 0>
10693
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
107-
__unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) {
94+
__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {
10895
auto __range = std::__unwrap_range(__first, std::move(__last));
10996
auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first));
11097
return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)),
@@ -115,24 +102,12 @@ template <class _Algorithm,
115102
class _InIter,
116103
class _Sent,
117104
class _OutIter,
118-
__enable_if_t<!__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0>
105+
__enable_if_t<!__can_rewrap<_InIter, _OutIter>::value, int> = 0>
119106
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
120-
__unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) {
107+
__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {
121108
return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first));
122109
}
123110

124-
template <class _AlgPolicy,
125-
class _NaiveAlgorithm,
126-
class _OptimizedAlgorithm,
127-
class _InIter,
128-
class _Sent,
129-
class _OutIter>
130-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
131-
__dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) {
132-
using _Algorithm = __overload<_NaiveAlgorithm, _OptimizedAlgorithm>;
133-
return std::__unwrap_and_dispatch<_Algorithm>(std::move(__first), std::move(__last), std::move(__out_first));
134-
}
135-
136111
_LIBCPP_END_NAMESPACE_STD
137112

138113
_LIBCPP_POP_MACROS

libcxx/include/__algorithm/move.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIte
3434
__move(_InIter __first, _Sent __last, _OutIter __result);
3535

3636
template <class _AlgPolicy>
37-
struct __move_loop {
37+
struct __move_impl {
3838
template <class _InIter, class _Sent, class _OutIter>
3939
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
4040
operator()(_InIter __first, _Sent __last, _OutIter __result) const {
@@ -95,9 +95,7 @@ struct __move_loop {
9595
__local_first = _Traits::__begin(++__segment_iterator);
9696
}
9797
}
98-
};
9998

100-
struct __move_trivial {
10199
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
102100
template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
103101
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
@@ -109,7 +107,7 @@ struct __move_trivial {
109107
template <class _AlgPolicy, class _InIter, class _Sent, class _OutIter>
110108
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
111109
__move(_InIter __first, _Sent __last, _OutIter __result) {
112-
return std::__dispatch_copy_or_move<_AlgPolicy, __move_loop<_AlgPolicy>, __move_trivial>(
110+
return std::__copy_move_unwrap_iters<__move_impl<_AlgPolicy> >(
113111
std::move(__first), std::move(__last), std::move(__result));
114112
}
115113

libcxx/include/__algorithm/move_backward.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_BidirectionalIterator1
3333
__move_backward(_BidirectionalIterator1 __first, _Sentinel __last, _BidirectionalIterator2 __result);
3434

3535
template <class _AlgPolicy>
36-
struct __move_backward_loop {
36+
struct __move_backward_impl {
3737
template <class _InIter, class _Sent, class _OutIter>
3838
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, _OutIter>
3939
operator()(_InIter __first, _Sent __last, _OutIter __result) const {
@@ -104,9 +104,7 @@ struct __move_backward_loop {
104104
__local_last = _Traits::__end(--__segment_iterator);
105105
}
106106
}
107-
};
108107

109-
struct __move_backward_trivial {
110108
// At this point, the iterators have been unwrapped so any `contiguous_iterator` has been unwrapped to a pointer.
111109
template <class _In, class _Out, __enable_if_t<__can_lower_move_assignment_to_memmove<_In, _Out>::value, int> = 0>
112110
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
@@ -122,7 +120,7 @@ __move_backward(_BidirectionalIterator1 __first, _Sentinel __last, _Bidirectiona
122120
std::is_copy_constructible<_BidirectionalIterator1>::value,
123121
"Iterators must be copy constructible.");
124122

125-
return std::__dispatch_copy_or_move<_AlgPolicy, __move_backward_loop<_AlgPolicy>, __move_backward_trivial>(
123+
return std::__copy_move_unwrap_iters<__move_backward_impl<_AlgPolicy> >(
126124
std::move(__first), std::move(__last), std::move(__result));
127125
}
128126

0 commit comments

Comments
 (0)