[libcxx-commits] [libcxx] [libc++] Optimize lexicographical_compare (PR #65279)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 4 17:44:09 PDT 2023


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/65279:

>From fec7ee6df925ff454ccdf6b12d5790dd2c2a2596 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 4 Sep 2023 10:46:58 -0700
Subject: [PATCH] [libc++] Optimize lexicographical_compare

---
 libcxx/benchmarks/CMakeLists.txt              |  1 +
 .../lexicographical_compare.bench.cpp         | 41 +++++++++
 libcxx/include/__algorithm/comp.h             |  3 +
 .../__algorithm/lexicographical_compare.h     | 81 +++++++++++------
 .../ranges_lexicographical_compare.h          | 27 +++---
 libcxx/include/__functional/operations.h      |  8 ++
 .../include/__functional/ranges_operations.h  |  3 +
 .../include/__type_traits/predicate_traits.h  |  3 +
 .../lexicographical_compare.pass.cpp          | 87 +++++++------------
 libcxx/utils/data/ignore_format.txt           |  1 -
 10 files changed, 162 insertions(+), 93 deletions(-)
 create mode 100644 libcxx/benchmarks/algorithms/lexicographical_compare.bench.cpp

diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt
index 92f295ef3990084..fad1cb4dbe75de4 100644
--- a/libcxx/benchmarks/CMakeLists.txt
+++ b/libcxx/benchmarks/CMakeLists.txt
@@ -155,6 +155,7 @@ set(BENCHMARK_TESTS
     algorithms.partition_point.bench.cpp
     algorithms/equal.bench.cpp
     algorithms/find.bench.cpp
+    algorithms/lexicographical_compare.bench.cpp
     algorithms/lower_bound.bench.cpp
     algorithms/make_heap.bench.cpp
     algorithms/make_heap_then_sort_heap.bench.cpp
diff --git a/libcxx/benchmarks/algorithms/lexicographical_compare.bench.cpp b/libcxx/benchmarks/algorithms/lexicographical_compare.bench.cpp
new file mode 100644
index 000000000000000..bbbbbd3dbff4dea
--- /dev/null
+++ b/libcxx/benchmarks/algorithms/lexicographical_compare.bench.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <vector>
+
+template <class T>
+static void bm_lexicographical_compare(benchmark::State& state) {
+  std::vector<T> vec1(state.range(), '1');
+  std::vector<T> vec2(state.range(), '1');
+
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(vec1);
+    benchmark::DoNotOptimize(vec2);
+    benchmark::DoNotOptimize(std::lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()));
+  }
+}
+BENCHMARK(bm_lexicographical_compare<unsigned char>)->DenseRange(1, 8)->Range(16, 1 << 20);
+BENCHMARK(bm_lexicographical_compare<int>)->DenseRange(1, 8)->Range(16, 1 << 20);
+
+template <class T>
+static void bm_ranges_lexicographical_compare(benchmark::State& state) {
+  std::vector<T> vec1(state.range(), '1');
+  std::vector<T> vec2(state.range(), '1');
+
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(vec1);
+    benchmark::DoNotOptimize(vec2);
+    benchmark::DoNotOptimize(std::ranges::lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()));
+  }
+}
+BENCHMARK(bm_ranges_lexicographical_compare<unsigned char>)->DenseRange(1, 8)->Range(16, 1 << 20);
+BENCHMARK(bm_ranges_lexicographical_compare<int>)->DenseRange(1, 8)->Range(16, 1 << 20);
+
+BENCHMARK_MAIN();
diff --git a/libcxx/include/__algorithm/comp.h b/libcxx/include/__algorithm/comp.h
index 9474536615ffb67..b80e2056c8f90f4 100644
--- a/libcxx/include/__algorithm/comp.h
+++ b/libcxx/include/__algorithm/comp.h
@@ -42,6 +42,9 @@ struct __less<void, void> {
   }
 };
 
+template <class _Lhs, class _Rhs>
+struct __is_trivial_less_than_predicate<__less<>, _Lhs, _Rhs> : true_type {};
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___ALGORITHM_COMP_H
diff --git a/libcxx/include/__algorithm/lexicographical_compare.h b/libcxx/include/__algorithm/lexicographical_compare.h
index 62b72edc80f7731..d607baa825988e9 100644
--- a/libcxx/include/__algorithm/lexicographical_compare.h
+++ b/libcxx/include/__algorithm/lexicographical_compare.h
@@ -11,50 +11,79 @@
 
 #include <__algorithm/comp.h>
 #include <__algorithm/comp_ref_type.h>
+#include <__algorithm/min.h>
+#include <__algorithm/unwrap_iter.h>
 #include <__config>
+#include <__functional/identity.h>
 #include <__iterator/iterator_traits.h>
+#include <__string/constexpr_c_functions.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_trivially_lexicographically_comparable.h>
+#include <__type_traits/predicate_traits.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Compare, class _InputIterator1, class _InputIterator2>
+template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Comp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __lexicographical_compare(
+    _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp& __comp, _Proj1& __proj1, _Proj2& __proj2) {
+  while (__first2 != __last2) {
+    if (__first1 == __last1 ||
+        std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
+      return true;
+    if (std::__invoke(__comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
+      return false;
+    ++__first1;
+    ++__first2;
+  }
+  return false;
+}
+
+#if _LIBCPP_STD_VER >= 20
+template <class _Tp, class _Up, class _Proj1, class _Proj2, class _Comp>
+  requires(__libcpp_is_trivially_lexicographically_comparable<_Tp, _Up>::value && __is_identity<_Proj1>::value &&
+           __is_identity<_Proj2>::value && __is_trivial_less_than_predicate<_Comp, _Tp, _Up>::value)
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
-__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
-                          _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
-{
-    for (; __first2 != __last2; ++__first1, (void) ++__first2)
-    {
-        if (__first1 == __last1 || __comp(*__first1, *__first2))
-            return true;
-        if (__comp(*__first2, *__first1))
-            return false;
-    }
-    return false;
+__lexicographical_compare(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up* __last2, _Comp&, _Proj1&, _Proj2&) {
+  if (auto __res = std::__constexpr_memcmp(
+             __first1, __first2, __element_count(std::min(__last1 - __first1, __last2 - __first2))))
+    return __res < 0;
+  return __last1 - __first1 < __last2 - __first2;
 }
+#endif
 
 template <class _InputIterator1, class _InputIterator2, class _Compare>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
-bool
-lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
-                        _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
-{
-    return _VSTD::__lexicographical_compare<__comp_ref_type<_Compare> >(__first1, __last1, __first2, __last2, __comp);
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool lexicographical_compare(
+    _InputIterator1 __first1,
+    _InputIterator1 __last1,
+    _InputIterator2 __first2,
+    _InputIterator2 __last2,
+    _Compare __comp) {
+  __identity __proj;
+  return std::__lexicographical_compare(
+      std::__unwrap_iter(__first1),
+      std::__unwrap_iter(__last1),
+      std::__unwrap_iter(__first2),
+      std::__unwrap_iter(__last2),
+      __comp,
+      __proj,
+      __proj);
 }
 
 template <class _InputIterator1, class _InputIterator2>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
-bool
-lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
-                        _InputIterator2 __first2, _InputIterator2 __last2)
-{
-    return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2, __less<>());
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool lexicographical_compare(
+    _InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
+  return std::lexicographical_compare(__first1, __last1, __first2, __last2, __less<>());
 }
 
 _LIBCPP_END_NAMESPACE_STD
 
+_LIBCPP_POP_MACROS
+
 #endif // _LIBCPP___ALGORITHM_LEXICOGRAPHICAL_COMPARE_H
diff --git a/libcxx/include/__algorithm/ranges_lexicographical_compare.h b/libcxx/include/__algorithm/ranges_lexicographical_compare.h
index 5b843dfd7b31393..b0b841608333697 100644
--- a/libcxx/include/__algorithm/ranges_lexicographical_compare.h
+++ b/libcxx/include/__algorithm/ranges_lexicographical_compare.h
@@ -9,6 +9,8 @@
 #ifndef _LIBCPP___ALGORITHM_RANGES_LEXICOGRAPHICAL_COMPARE_H
 #define _LIBCPP___ALGORITHM_RANGES_LEXICOGRAPHICAL_COMPARE_H
 
+#include <__algorithm/lexicographical_compare.h>
+#include <__algorithm/unwrap_range.h>
 #include <__config>
 #include <__functional/identity.h>
 #include <__functional/invoke.h>
@@ -31,7 +33,7 @@ namespace ranges {
 namespace __lexicographical_compare {
 struct __fn {
   template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Comp>
-  _LIBCPP_HIDE_FROM_ABI constexpr static bool __lexicographical_compare_impl(
+  static _LIBCPP_HIDE_FROM_ABI constexpr bool __lexicographical_compare_unwrap(
       _Iter1 __first1,
       _Sent1 __last1,
       _Iter2 __first2,
@@ -39,15 +41,16 @@ struct __fn {
       _Comp& __comp,
       _Proj1& __proj1,
       _Proj2& __proj2) {
-    while (__first2 != __last2) {
-      if (__first1 == __last1 || std::invoke(__comp, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
-        return true;
-      if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1)))
-        return false;
-      ++__first1;
-      ++__first2;
-    }
-    return false;
+    auto [__first1_un, __last1_un] = std::__unwrap_range(std::move(__first1), std::move(__last1));
+    auto [__first2_un, __last2_un] = std::__unwrap_range(std::move(__first2), std::move(__last2));
+    return std::__lexicographical_compare(
+        std::move(__first1_un),
+        std::move(__last1_un),
+        std::move(__first2_un),
+        std::move(__last2_un),
+        __comp,
+        __proj1,
+        __proj2);
   }
 
   template <input_iterator _Iter1,
@@ -65,7 +68,7 @@ struct __fn {
       _Comp __comp   = {},
       _Proj1 __proj1 = {},
       _Proj2 __proj2 = {}) const {
-    return __lexicographical_compare_impl(
+    return __lexicographical_compare_unwrap(
         std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __comp, __proj1, __proj2);
   }
 
@@ -77,7 +80,7 @@ struct __fn {
                 _Comp = ranges::less>
   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
       _Range1&& __range1, _Range2&& __range2, _Comp __comp = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
-    return __lexicographical_compare_impl(
+    return __lexicographical_compare_unrwap(
         ranges::begin(__range1),
         ranges::end(__range1),
         ranges::begin(__range2),
diff --git a/libcxx/include/__functional/operations.h b/libcxx/include/__functional/operations.h
index 6cdb89d6b449bcd..d41c80c69243764 100644
--- a/libcxx/include/__functional/operations.h
+++ b/libcxx/include/__functional/operations.h
@@ -418,6 +418,14 @@ struct _LIBCPP_TEMPLATE_VIS less<void>
 };
 #endif
 
+template <class _Tp>
+struct __is_trivial_less_than_predicate<less<_Tp>, _Tp, _Tp> : true_type {};
+
+#if _LIBCPP_STD_VER >= 14
+template <class _Tp, class _Up>
+struct __is_trivial_less_than_predicate<less<>, _Tp, _Up> : true_type {};
+#endif
+
 #if _LIBCPP_STD_VER >= 14
 template <class _Tp = void>
 #else
diff --git a/libcxx/include/__functional/ranges_operations.h b/libcxx/include/__functional/ranges_operations.h
index c344fc38f98ddd9..5da79655a65a7d9 100644
--- a/libcxx/include/__functional/ranges_operations.h
+++ b/libcxx/include/__functional/ranges_operations.h
@@ -98,6 +98,9 @@ struct greater_equal {
 template <class _Lhs, class _Rhs>
 struct __is_trivial_equality_predicate<ranges::equal_to, _Lhs, _Rhs> : true_type {};
 
+template <class _Lhs, class _Rhs>
+struct __is_trivial_less_than_predicate<ranges::less, _Lhs, _Rhs> : true_type {};
+
 #endif // _LIBCPP_STD_VER >= 20
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__type_traits/predicate_traits.h b/libcxx/include/__type_traits/predicate_traits.h
index 872608e6ac3be3f..815847cce41e078 100644
--- a/libcxx/include/__type_traits/predicate_traits.h
+++ b/libcxx/include/__type_traits/predicate_traits.h
@@ -21,6 +21,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Pred, class _Lhs, class _Rhs>
 struct __is_trivial_equality_predicate : false_type {};
 
+template <class _Pred, class _Lhs, class _Rhs>
+struct __is_trivial_less_than_predicate : false_type {};
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___TYPE_TRAITS_PREDICATE_TRAITS
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp
index 10e45df7cf9aa32..52f72db05b56cc4 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp
@@ -20,66 +20,45 @@
 #include "test_macros.h"
 #include "test_iterators.h"
 
-#if TEST_STD_VER > 17
-TEST_CONSTEXPR bool test_constexpr() {
-    int ia[] = {1, 2, 3};
-    int ib[] = {1, 3, 5, 2, 4, 6};
+template <class T, class Iter1>
+struct Test {
+  template <class Iter2>
+  TEST_CONSTEXPR_CXX20 void operator()() {
+    T ia[]            = {1, 2, 3, 4};
+    const unsigned sa = sizeof(ia) / sizeof(ia[0]);
+    T ib[]            = {1, 2, 3};
+    assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib), Iter2(ib + 2)));
+    assert(std::lexicographical_compare(Iter1(ib), Iter1(ib + 2), Iter2(ia), Iter2(ia + sa)));
+    assert(!std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib), Iter2(ib + 3)));
+    assert(std::lexicographical_compare(Iter1(ib), Iter1(ib + 3), Iter2(ia), Iter2(ia + sa)));
+    assert(std::lexicographical_compare(Iter1(ia), Iter1(ia + sa), Iter2(ib + 1), Iter2(ib + 3)));
+    assert(!std::lexicographical_compare(Iter1(ib + 1), Iter1(ib + 3), Iter2(ia), Iter2(ia + sa)));
+  }
+};
 
-    return  std::lexicographical_compare(std::begin(ia), std::end(ia), std::begin(ib), std::end(ib))
-        && !std::lexicographical_compare(std::begin(ib), std::end(ib), std::begin(ia), std::end(ia))
-           ;
-    }
-#endif
-
-template <class Iter1, class Iter2>
-void
-test()
-{
-    int ia[] = {1, 2, 3, 4};
-    const unsigned sa = sizeof(ia)/sizeof(ia[0]);
-    int ib[] = {1, 2, 3};
-    assert(!std::lexicographical_compare(Iter1(ia),   Iter1(ia+sa), Iter2(ib),   Iter2(ib+2)));
-    assert( std::lexicographical_compare(Iter1(ib),   Iter1(ib+2),  Iter2(ia),   Iter2(ia+sa)));
-    assert(!std::lexicographical_compare(Iter1(ia),   Iter1(ia+sa), Iter2(ib),   Iter2(ib+3)));
-    assert( std::lexicographical_compare(Iter1(ib),   Iter1(ib+3),  Iter2(ia),   Iter2(ia+sa)));
-    assert( std::lexicographical_compare(Iter1(ia),   Iter1(ia+sa), Iter2(ib+1), Iter2(ib+3)));
-    assert(!std::lexicographical_compare(Iter1(ib+1), Iter1(ib+3),  Iter2(ia),   Iter2(ia+sa)));
-}
+template <class T>
+struct TestIter {
+  template <class Iter1>
+  TEST_CONSTEXPR_CXX20 bool operator()() {
+    types::for_each(types::cpp17_input_iterator_list<T*>(), Test<T, Iter1>());
 
-int main(int, char**)
-{
-    test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*> >();
-    test<cpp17_input_iterator<const int*>, forward_iterator<const int*> >();
-    test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*> >();
-    test<cpp17_input_iterator<const int*>, random_access_iterator<const int*> >();
-    test<cpp17_input_iterator<const int*>, const int*>();
+    return true;
+  }
+};
 
-    test<forward_iterator<const int*>, cpp17_input_iterator<const int*> >();
-    test<forward_iterator<const int*>, forward_iterator<const int*> >();
-    test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
-    test<forward_iterator<const int*>, random_access_iterator<const int*> >();
-    test<forward_iterator<const int*>, const int*>();
+TEST_CONSTEXPR_CXX20 bool test() {
+  types::for_each(types::cpp17_input_iterator_list<const int*>(), TestIter<const int>());
+  types::for_each(types::cpp17_input_iterator_list<const char*>(), TestIter<const char>());
+  types::for_each(types::cpp17_input_iterator_list<unsigned char*>(), TestIter<unsigned char>());
 
-    test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*> >();
-    test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
-    test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
-    test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
-    test<bidirectional_iterator<const int*>, const int*>();
-
-    test<random_access_iterator<const int*>, cpp17_input_iterator<const int*> >();
-    test<random_access_iterator<const int*>, forward_iterator<const int*> >();
-    test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
-    test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
-    test<random_access_iterator<const int*>, const int*>();
+  return true;
+}
 
-    test<const int*, cpp17_input_iterator<const int*> >();
-    test<const int*, forward_iterator<const int*> >();
-    test<const int*, bidirectional_iterator<const int*> >();
-    test<const int*, random_access_iterator<const int*> >();
-    test<const int*, const int*>();
+int main(int, char**) {
+  test();
 
-#if TEST_STD_VER > 17
-    static_assert(test_constexpr());
+#if TEST_STD_VER >= 20
+  static_assert(test());
 #endif
 
   return 0;
diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 3caf9d45c783ec4..3211c2668530857 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -31,7 +31,6 @@ libcxx/include/__algorithm/is_sorted.h
 libcxx/include/__algorithm/is_sorted_until.h
 libcxx/include/__algorithm/iterator_operations.h
 libcxx/include/__algorithm/iter_swap.h
-libcxx/include/__algorithm/lexicographical_compare.h
 libcxx/include/__algorithm/lower_bound.h
 libcxx/include/__algorithm/make_heap.h
 libcxx/include/__algorithm/make_projected.h



More information about the libcxx-commits mailing list