-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Optimize the implementation of std::find_first_of to use any_of #129319 #129574
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-libcxx Author: Ali Raeisdanaei (aliraeisdanaei) ChangesFull diff: https://github.com/llvm/llvm-project/pull/129574.diff 1 Files Affected:
diff --git a/libcxx/include/__algorithm/find_first_of.h b/libcxx/include/__algorithm/find_first_of.h
index 45ec133154371..54c0dbd103ddb 100644
--- a/libcxx/include/__algorithm/find_first_of.h
+++ b/libcxx/include/__algorithm/find_first_of.h
@@ -26,11 +26,9 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_fir
_ForwardIterator2 __first2,
_ForwardIterator2 __last2,
_BinaryPredicate&& __pred) {
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__first1, *__j))
- return __first1;
- return __last1;
+ return std::find_if(first1, last1, [&](const auto& x) {
+ return std::any_of(first2, last2, [&](const auto& y) { return x == y; });
+ });
}
template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide some benchmarks to show whether there are any performance improvements?
return __first1; | ||
return __last1; | ||
return std::find_if(first1, last1, [&](const auto& x) { | ||
return std::any_of(first2, last2, [&](const auto& y) { return x == y; }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this just std::find
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No std::find
would returns only for the first value; whereas, this one returns where any of the values in the needle are found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm talking about that std::any_of
call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right std::find
and std::any_of
have the same function; however, std::find
returns an iterator, and std::any_of
returns a predicate which is needed in the std::find_any
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally std::find
does not work for custom predicates. (I may be totally wrong).
Sure, but this may take me a few days. |
It seems the failing checks are due to regex checks. I haven't touched it and keep my implementation within libc++. |
Can you provide me some resources on how to build the benchmarks? |
No description provided.