[libcxx-commits] [libcxx] [libc++] Backport segmented iterator optimization for std::for_each to C++03 (PR #134960)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 10 13:12:19 PDT 2025


================
@@ -26,28 +26,43 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIterator, class _Function>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
-for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
+template <class _InputIterator, class _Sent, class _Func>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __for_each(_InputIterator __first, _Sent __last, _Func& __f) {
   for (; __first != __last; ++__first)
     __f(*__first);
-  return __f;
 }
 
-// __movable_box is available in C++20, but is actually a copyable-box, so optimization is only correct in C++23
-#if _LIBCPP_STD_VER >= 23
-template <class _SegmentedIterator, class _Function>
-  requires __is_segmented_iterator<_SegmentedIterator>::value
-_LIBCPP_HIDE_FROM_ABI constexpr _Function
-for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
-  ranges::__movable_box<_Function> __wrapped_func(in_place, std::move(__func));
-  std::__for_each_segment(__first, __last, [&](auto __lfirst, auto __llast) {
-    __wrapped_func =
-        ranges::__movable_box<_Function>(in_place, std::for_each(__lfirst, __llast, std::move(*__wrapped_func)));
-  });
-  return std::move(*__wrapped_func);
+// __segment_processor handles the per-segment processing by applying the function object __func_ to each
+// element within the segment.
+template <class _SegmentedIterator, class _Func>
+struct __segment_processor {
+  using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_SegmentedIterator>;
+
+  _Func& __func_;
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __segment_processor(_Func& __f) : __func_(__f) {}
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
+  operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
+    std::__for_each(__lfirst, __llast, __func_);
+  }
+};
+
+template <class _SegmentedIterator,
+          class _Function,
+          __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
+__for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
+  std::__for_each_segment(__first, __last, std::__segment_processor<_SegmentedIterator, _Function>(__func));
----------------
winner245 wrote:

Thanks for noticing the unnecessary copying of the functor object. It was an oversight on my part, and I've fixed it. Following your suggestion, I've also added a regression test to ensure the issue won't come up in the future.

Regarding the back-porting, I completely agree with you that if we aim to back-port to C++11, then this could be significantly simplified by using a lambda. However, I have a preference to back-porting this to C++03, because I really want the segmented iterator optimization to be consistently supported in various algorithms. For example, existing optimizations for `std::copy`, `std::copy_backward`, and ``std::move`, and `std::find` already support C++03. I feel like it would be nice to consistently support the optimization for `for_each` in C++03. Another reason for supporting C++03 is that I want the optimization for `for_each` to be more broadly applicable, so that when other algorithms forward to `for_each`, they don't get the restriction that the optimization is not supported for C++03 (otherwise, I would have to add `_LIBCPP_STD_VER >= 11` wherever an algorithm forwards to `for_each`). 

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


More information about the libcxx-commits mailing list