[libcxx-commits] [libcxx] [libc++] Optimize ranges::{for_each, for_each_n} for segmented iterators (PR #132896)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Mar 26 08:30:31 PDT 2025
================
@@ -26,28 +27,43 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
-template <class _InputIterator, class _Function>
+template <class, class _InputIterator, class _Sent, class _Function>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
-for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
+__for_each(_InputIterator __first, _Sent __last, _Function& __f) {
for (; __first != __last; ++__first)
__f(*__first);
- return __f;
+ return std::move(__f);
+}
+
+template <class _InputIterator, class _Function>
+struct _ForeachSegment {
+ using _Traits _LIBCPP_NODEBUG = __segmented_iterator_traits<_InputIterator>;
+
+ _Function& __func_;
+
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit _ForeachSegment(_Function& __func) : __func_(__func) {}
+
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
+ operator()(typename _Traits::__local_iterator __lfirst, typename _Traits::__local_iterator __llast) {
+ std::__for_each<_ClassicAlgPolicy>(__lfirst, __llast, __func_);
+ }
+};
+
+template <class,
+ class _SegmentedIterator,
+ class _Function,
+ __enable_if_t<__is_segmented_iterator<_SegmentedIterator>::value, int> = 0>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Function
+__for_each(_SegmentedIterator __first, _SegmentedIterator __last, _Function& __func) {
+ std::__for_each_segment(__first, __last, _ForeachSegment<_SegmentedIterator, _Function>(__func));
+ return std::move(__func);
}
-// __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);
+template <class _InputIterator, class _Function>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Function
+for_each(_InputIterator __first, _InputIterator __last, _Function __f) {
+ return std::__for_each<_ClassicAlgPolicy>(__first, __last, __f);
}
-#endif // _LIBCPP_STD_VER >= 23
----------------
winner245 wrote:
IIUC, we used `__movable_box` to avoid copying of the function object. However, `__movable_box` is only moving in C++23. This impose a limitation that the segmented iterator optimization only applies to C++23 and above. I am removing the dependence on `__movable_box` so that our optimization applies to a wider standard mode.
https://github.com/llvm/llvm-project/pull/132896
More information about the libcxx-commits
mailing list