[libcxx-commits] [libcxx] [libc++] Tiny optimizations for is_permutation (PR #129565)
Imad Aldij via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Mar 24 11:45:12 PDT 2025
https://github.com/imdj updated https://github.com/llvm/llvm-project/pull/129565
>From 1b7878a9157d26fb9ea04f70b8f2997867d78552 Mon Sep 17 00:00:00 2001
From: Imad Aldij <69906094+imdj at users.noreply.github.com>
Date: Fri, 7 Mar 2025 06:41:17 +0200
Subject: [PATCH 1/5] [libc++] Tiny optimizations for is_permutation
Replace hand-written loops with vectorized std::mismatch, std::find_if, and std::count_if
---
libcxx/include/__algorithm/is_permutation.h | 41 +++++++++++----------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/libcxx/include/__algorithm/is_permutation.h b/libcxx/include/__algorithm/is_permutation.h
index 1afb11596bc6b..b1d597f8e1e40 100644
--- a/libcxx/include/__algorithm/is_permutation.h
+++ b/libcxx/include/__algorithm/is_permutation.h
@@ -11,9 +11,13 @@
#define _LIBCPP___ALGORITHM_IS_PERMUTATION_H
#include <__algorithm/comp.h>
+#include <__algorithm/count_if.h>
+#include <__algorithm/find_if.h>
#include <__algorithm/iterator_operations.h>
+#include <__algorithm/mismatch.h>
#include <__config>
#include <__functional/identity.h>
+#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
#include <__iterator/distance.h>
#include <__iterator/iterator_traits.h>
@@ -82,28 +86,28 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(
for (auto __i = __first1; __i != __last1; ++__i) {
// Have we already counted the number of *__i in [f1, l1)?
- auto __match = __first1;
- for (; __match != __i; ++__match) {
- if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i)))
- break;
- }
+ auto __match = std::find_if(__first1, __i, [&](typename iterator_traits<_Iter1>::reference __x) -> bool {
+ return std::__invoke(__pred, std::__invoke(__proj1, __x), std::__invoke(__proj1, *__i));
+ });
if (__match == __i) {
// Count number of *__i in [f2, l2)
- _D1 __c2 = 0;
- for (auto __j = __first2; __j != __last2; ++__j) {
- if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j)))
- ++__c2;
- }
+ __identity __ident1;
+ auto __predicate1 = [&](typename iterator_traits<_Iter2>::reference __x) -> bool {
+ return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, __x));
+ };
+ _D1 __c2 = std::__count_if<_AlgPolicy>(__first2, __last2, __predicate1, __ident1);
if (__c2 == 0)
return false;
// Count number of *__i in [__i, l1) (we can start with 1)
- _D1 __c1 = 1;
- for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) {
- if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j)))
- ++__c1;
- }
+ __identity __ident2;
+ auto __predicate2 = [&](typename iterator_traits<_Iter1>::reference __x) -> bool {
+ return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, __x));
+ };
+ auto __start = _IterOps<_AlgPolicy>::next(__i);
+ _D1 __c1 = std::__count_if<_AlgPolicy>(__start, __last1, __predicate2, __ident2);
+ __c1 += 1;
if (__c1 != __c2)
return false;
}
@@ -117,10 +121,9 @@ template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _Fo
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) {
// Shorten sequences as much as possible by lopping of any equal prefix.
- for (; __first1 != __last1; ++__first1, (void)++__first2) {
- if (!__pred(*__first1, *__first2))
- break;
- }
+ auto __result = std::mismatch(__first1, __last1, __first2, std::ref(__pred));
+ __first1 = __result.first;
+ __first2 = __result.second;
if (__first1 == __last1)
return true;
>From eaa81487e438a41f54c52b438f45e95bfc6ff1a0 Mon Sep 17 00:00:00 2001
From: Imad Aldij <69906094+imdj at users.noreply.github.com>
Date: Sun, 9 Mar 2025 12:44:51 +0200
Subject: [PATCH 2/5] Adjust for readability and performance
---
libcxx/include/__algorithm/is_permutation.h | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__algorithm/is_permutation.h b/libcxx/include/__algorithm/is_permutation.h
index b1d597f8e1e40..f8fed816c9b77 100644
--- a/libcxx/include/__algorithm/is_permutation.h
+++ b/libcxx/include/__algorithm/is_permutation.h
@@ -83,31 +83,31 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(
_Proj1&& __proj1,
_Proj2&& __proj2) {
using _D1 = __iter_diff_t<_Iter1>;
+ using _Ref1 = typename iterator_traits<_Iter1>::reference;
+ using _Ref2 = typename iterator_traits<_Iter2>::reference;
+ __identity __ident;
for (auto __i = __first1; __i != __last1; ++__i) {
// Have we already counted the number of *__i in [f1, l1)?
- auto __match = std::find_if(__first1, __i, [&](typename iterator_traits<_Iter1>::reference __x) -> bool {
+ auto __match = std::find_if(__first1, __i, [&](_Ref1 __x) -> bool {
return std::__invoke(__pred, std::__invoke(__proj1, __x), std::__invoke(__proj1, *__i));
});
if (__match == __i) {
// Count number of *__i in [f2, l2)
- __identity __ident1;
- auto __predicate1 = [&](typename iterator_traits<_Iter2>::reference __x) -> bool {
+ auto __predicate2 = [&](_Ref2 __x) -> bool {
return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, __x));
};
- _D1 __c2 = std::__count_if<_AlgPolicy>(__first2, __last2, __predicate1, __ident1);
+ _D1 __c2 = std::__count_if<_AlgPolicy>(__first2, __last2, __predicate2, __ident);
if (__c2 == 0)
return false;
// Count number of *__i in [__i, l1) (we can start with 1)
- __identity __ident2;
- auto __predicate2 = [&](typename iterator_traits<_Iter1>::reference __x) -> bool {
+ auto __predicate1 = [&](_Ref1 __x) -> bool {
return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, __x));
};
auto __start = _IterOps<_AlgPolicy>::next(__i);
- _D1 __c1 = std::__count_if<_AlgPolicy>(__start, __last1, __predicate2, __ident2);
- __c1 += 1;
+ _D1 __c1 = 1 + std::__count_if<_AlgPolicy>(__start, __last1, __predicate1, __ident);
if (__c1 != __c2)
return false;
}
>From ef1a6d6ad437ffd5e2166e1f886080bb7c54c071 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Thu, 20 Mar 2025 05:29:34 +0200
Subject: [PATCH 3/5] Fix typos in is_permutation
---
libcxx/include/__algorithm/is_permutation.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__algorithm/is_permutation.h b/libcxx/include/__algorithm/is_permutation.h
index f8fed816c9b77..73a3d486f78a9 100644
--- a/libcxx/include/__algorithm/is_permutation.h
+++ b/libcxx/include/__algorithm/is_permutation.h
@@ -120,7 +120,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(
template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate>
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) {
- // Shorten sequences as much as possible by lopping of any equal prefix.
+ // Shorten sequences as much as possible by lopping off any equal prefix.
auto __result = std::mismatch(__first1, __last1, __first2, std::ref(__pred));
__first1 = __result.first;
__first2 = __result.second;
@@ -163,7 +163,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
_Proj1&& __proj1,
_Proj2&& __proj2,
/*_ConstTimeDistance=*/false_type) {
- // Shorten sequences as much as possible by lopping of any equal prefix.
+ // Shorten sequences as much as possible by lopping off any equal prefix.
while (__first1 != __last1 && __first2 != __last2) {
if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
break;
>From 6652e66fc1f1e34b89624d04a8a7232e03a795d0 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Thu, 20 Mar 2025 11:29:22 +0200
Subject: [PATCH 4/5] Substitute second loop with mismatch
---
libcxx/include/__algorithm/is_permutation.h | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__algorithm/is_permutation.h b/libcxx/include/__algorithm/is_permutation.h
index 73a3d486f78a9..b02e7bf22ef99 100644
--- a/libcxx/include/__algorithm/is_permutation.h
+++ b/libcxx/include/__algorithm/is_permutation.h
@@ -164,12 +164,10 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
_Proj2&& __proj2,
/*_ConstTimeDistance=*/false_type) {
// Shorten sequences as much as possible by lopping off any equal prefix.
- while (__first1 != __last1 && __first2 != __last2) {
- if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
- break;
- ++__first1;
- ++__first2;
- }
+ auto __result = std::__mismatch(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
+
+ __first1 = __result.first;
+ __first2 = __result.second;
if (__first1 == __last1)
return __first2 == __last2;
>From dc1b93080203245bcd9a1284a841bfe8ee88ed0d Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Mon, 24 Mar 2025 20:44:38 +0200
Subject: [PATCH 5/5] Remove preemptive iterator advancement for better
optimizations
---
libcxx/include/__algorithm/is_permutation.h | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/libcxx/include/__algorithm/is_permutation.h b/libcxx/include/__algorithm/is_permutation.h
index b02e7bf22ef99..5a8672663753a 100644
--- a/libcxx/include/__algorithm/is_permutation.h
+++ b/libcxx/include/__algorithm/is_permutation.h
@@ -102,12 +102,11 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(
if (__c2 == 0)
return false;
- // Count number of *__i in [__i, l1) (we can start with 1)
+ // Count number of *__i in [__i, l1)
auto __predicate1 = [&](_Ref1 __x) -> bool {
return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, __x));
};
- auto __start = _IterOps<_AlgPolicy>::next(__i);
- _D1 __c1 = 1 + std::__count_if<_AlgPolicy>(__start, __last1, __predicate1, __ident);
+ _D1 __c1 = std::__count_if<_AlgPolicy>(__i, __last1, __predicate1, __ident);
if (__c1 != __c2)
return false;
}
More information about the libcxx-commits
mailing list