Skip to content

[libc++][ranges] Optimize the performance of ranges::starts_with #84570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions libcxx/include/__algorithm/ranges_starts_with.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
#ifndef _LIBCPP___ALGORITHM_RANGES_STARTS_WITH_H
#define _LIBCPP___ALGORITHM_RANGES_STARTS_WITH_H

#include <__algorithm/in_in_result.h>
#include <__algorithm/ranges_equal.h>
#include <__algorithm/ranges_mismatch.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/ranges_operations.h>
#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
#include <__iterator/distance.h>
#include <__iterator/indirectly_comparable.h>
#include <__iterator/next.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/size.h>
#include <__utility/move.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down Expand Up @@ -49,14 +53,37 @@ struct __starts_with {
_Pred __pred = {},
_Proj1 __proj1 = {},
_Proj2 __proj2 = {}) {
return __mismatch::__go(
if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
auto __n1 = ranges::distance(__first1, __last1);
auto __n2 = ranges::distance(__first2, __last2);
if (__n2 == 0) {
return true;
}
if (__n2 > __n1) {
return false;
}

if constexpr (contiguous_iterator<_Iter1> && contiguous_iterator<_Iter2>) {
auto __end1 = ranges::next(__first1, __n2);
return ranges::equal(
std::move(__first1),
std::move(__end1),
std::move(__first2),
std::move(__last2),
std::ref(__pred),
std::ref(__proj1),
std::ref(__proj2));
}
}

return ranges::mismatch(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
__pred,
__proj1,
__proj2)
__last2,
std::ref(__pred),
std::ref(__proj1),
std::ref(__proj2))
.in2 == __last2;
}

Expand All @@ -68,17 +95,40 @@ struct __starts_with {
requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool
operator()(_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) {
return __mismatch::__go(
if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
auto __n1 = ranges::size(__range1);
auto __n2 = ranges::size(__range2);
if (__n2 == 0) {
return true;
}
if (__n2 > __n1) {
return false;
}

if constexpr (contiguous_range<_Range1> && contiguous_range<_Range2>) {
return ranges::equal(
ranges::begin(__range1),
ranges::next(ranges::begin(__range1), __n2),
ranges::begin(__range2),
ranges::end(__range2),
std::ref(__pred),
std::ref(__proj1),
std::ref(__proj2));
}
}

return ranges::mismatch(
ranges::begin(__range1),
ranges::end(__range1),
ranges::begin(__range2),
ranges::end(__range2),
__pred,
__proj1,
__proj2)
std::ref(__pred),
std::ref(__proj1),
std::ref(__proj2))
.in2 == ranges::end(__range2);
}
};

inline namespace __cpo {
inline constexpr auto starts_with = __starts_with{};
} // namespace __cpo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,17 @@ constexpr void test_iterators() {
constexpr bool test() {
types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter2>() {
types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iter1>() {
if constexpr (std::forward_iterator<Iter1> && std::forward_iterator<Iter2>)
if constexpr (std::forward_iterator<Iter1> && std::forward_iterator<Iter2>) {
test_iterators<Iter1, Iter1, Iter2, Iter2>();
if constexpr (std::forward_iterator<Iter2>)
}
if constexpr (std::forward_iterator<Iter2>) {
test_iterators<Iter1, sentinel_wrapper<Iter1>, Iter2, Iter2>();
test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, Iter2>();
if constexpr (std::forward_iterator<Iter1>)
}
if constexpr (std::forward_iterator<Iter1>) {
test_iterators<Iter1, Iter1, Iter2, sentinel_wrapper<Iter2>>();
test_iterators<Iter1, Iter1, Iter2, sized_sentinel<Iter2>>();
}
test_iterators<Iter1, sized_sentinel<Iter1>, Iter2, sized_sentinel<Iter2>>();
});
});
Expand Down