[libcxx-commits] [libcxx] [libc++][ranges] optimize the performance of `ranges::starts_with` (PR #84570)
Xiaoyang Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Mar 9 11:57:59 PST 2024
================
@@ -99,6 +100,10 @@ struct greater_equal {
// operator are of the same type
template <class _Tp, class _Up>
struct __desugars_to<__equal_tag, ranges::equal_to, _Tp, _Up> : true_type {};
+template <class _Tp, class _Up>
+struct __desugars_to<__equal_tag, reference_wrapper<ranges::equal_to>, _Tp, _Up> : true_type {};
+template <class _Tp, class _Up>
+struct __desugars_to<__equal_tag, reference_wrapper<const ranges::equal_to>, _Tp, _Up> : true_type {};
----------------
xiaoyang-sde wrote:
`ranges::equal` will invoke `__equal_impl`. The `__equal_impl` overload that invokes `__builtin_memcmp` requires that the predicate models `__desugars_to<__equal_tag>` and the projection models `__is_identity`.
```cpp
template <class _Tp,
class _Up,
class _Pred,
class _Proj1,
class _Proj2,
__enable_if_t<__desugars_to<__equal_tag, _Pred, _Tp, _Up>::value && __is_identity<_Proj1>::value &&
__is_identity<_Proj2>::value && !is_volatile<_Tp>::value && !is_volatile<_Up>::value &&
__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
int> = 0>
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
__equal_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up*, _Pred&, _Proj1&, _Proj2&) {
return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
}
```
These lines allows the user to pass the `std::ranges::equal_to` predicate wrapped in a `std::reference_wrapper`.
https://github.com/llvm/llvm-project/pull/84570
More information about the libcxx-commits
mailing list