Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

aliraeisdanaei
Copy link
Contributor

No description provided.

@aliraeisdanaei aliraeisdanaei requested a review from a team as a code owner March 3, 2025 19:15
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 3, 2025

@llvm/pr-subscribers-libcxx

Author: Ali Raeisdanaei (aliraeisdanaei)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/129574.diff

1 Files Affected:

  • (modified) libcxx/include/__algorithm/find_first_of.h (+3-5)
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>

@aliraeisdanaei
Copy link
Contributor Author

@ldionne I would really appreciate your feedback. Does my implementation align with what you meant by #129574 ?

@H-G-Hristov
Copy link
Contributor

@ldionne I would really appreciate your feedback. Does my implementation align with what you meant by #129574 ?

Do you mean to have the ".h" in the title?

@aliraeisdanaei aliraeisdanaei changed the title Optimize the implementation of std::find_first_of.h to use any_of #129319 Optimize the implementation of std::find_first_of to use any_of #129319 Mar 3, 2025
@aliraeisdanaei
Copy link
Contributor Author

@ldionne I would really appreciate your feedback. Does my implementation align with what you meant by #129574 ?

Do you mean to have the ".h" in the title?

No my terminal autofilled that

Copy link
Contributor

@philnik777 philnik777 left a 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; });
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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).

@aliraeisdanaei
Copy link
Contributor Author

Could you provide some benchmarks to show whether there are any performance improvements?

Sure, but this may take me a few days.

@aliraeisdanaei
Copy link
Contributor Author

It seems the failing checks are due to regex checks. I haven't touched it and keep my implementation within libc++.

@aliraeisdanaei
Copy link
Contributor Author

Could you provide some benchmarks to show whether there are any performance improvements?

Can you provide me some resources on how to build the benchmarks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants