[libcxx-commits] [libcxx] [libc++] Optimize ranges::copy for forward_iterator and segmented_iterator (PR #120134)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Sat Mar 22 20:30:52 PDT 2025


================
@@ -95,6 +117,64 @@ struct __copy_impl {
     }
   }
 
+  template <class _InIter,
+            class _Sent,
+            class _Cp,
+            __enable_if_t<(__has_forward_iterator_category<_InIter>::value ||
+                           __has_iterator_concept_convertible_to<_InIter, forward_iterator_tag>::value) &&
+                              is_convertible<typename iterator_traits<_InIter>::value_type, bool>::value,
+                          int> = 0>
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_InIter, __bit_iterator<_Cp, false> >
+  operator()(_InIter __first, _Sent __last, __bit_iterator<_Cp, false> __result) const {
----------------
winner245 wrote:

Thank you for the thought-provoking comment. As suggested, I've implemented segmented iterator inputs for `std::copy`. With `segmented_iterator` input, each input segment reduces to a `forward_iterator`-pair, which is the case this patch optimizes for. As a result, the performance improvements for `forward_iterator`-pair inputs also extend to `segmented_iterator` inputs, yielding a 9x speed-up in both cases. For a more detailed explanation, please refer to my updated PR description.

https://github.com/llvm/llvm-project/pull/120134


More information about the libcxx-commits mailing list