[libcxx-commits] [PATCH] D151274: [libc++] Optimize for_each for segmented iterators

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 5 08:22:03 PDT 2023


ldionne added a comment.

We seem to be using copy assignment of the function object but that isn't a requirement of the algorithm. I think this problem is on our side. @philnik we could do something like:

  diff --git a/libcxx/include/__algorithm/for_each.h b/libcxx/include/__algorithm/for_each.h
  index 5e273cf1b9b1..5afd25b337e7 100644
  --- a/libcxx/include/__algorithm/for_each.h
  +++ b/libcxx/include/__algorithm/for_each.h
  @@ -35,10 +35,11 @@ template <class _SegmentedIterator, class _Function>
     requires __is_segmented_iterator<_SegmentedIterator>::value
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
   for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function __func) {
  +  std::__movable_box<_Function> __tmp(in_place, std::move(__func));
     std::__for_each_segment(__first, __last, [&](auto __lfirst, auto __llast) {
  -    __func = std::for_each(__lfirst, __llast, std::move(__func));
  +    __tmp = std::__movable_box<_Function>(in_place, std::for_each(__lfirst, __llast, std::move(*__tmp)));
     });
  -  return __func;
  +  return std::move(*__tmp);
   }
   #endif // _LIBCPP_STD_VER >= 20
   


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151274/new/

https://reviews.llvm.org/D151274



More information about the libcxx-commits mailing list