[libcxx-commits] [libcxx] [libc++] Optimize std::for_each_n for segmented iterators (PR #135468)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 14 14:16:24 PDT 2025


================
@@ -10,32 +10,88 @@
 #ifndef _LIBCPP___ALGORITHM_FOR_EACH_N_H
 #define _LIBCPP___ALGORITHM_FOR_EACH_N_H
 
+#include <__algorithm/for_each.h>
+#include <__algorithm/for_each_n_segment.h>
 #include <__config>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/segmented_iterator.h>
+#include <__type_traits/enable_if.h>
 #include <__utility/convert_to_integral.h>
+#include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
-_LIBCPP_BEGIN_NAMESPACE_STD
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
 
-#if _LIBCPP_STD_VER >= 17
+_LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIterator, class _Size, class _Function>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _InputIterator
-for_each_n(_InputIterator __first, _Size __orig_n, _Function __f) {
+template <class _InputIterator,
+          class _Size,
+          class _Func,
----------------
winner245 wrote:

With the TODO comment addressed by the use of `_Or` with lazy evaluation, I noticed that I couldn't find an appropriate test case to fail the previous version without addressing this TODO. The reason is that `__segmented_iterator_traits<_JoinViewIterator>` is defined only for `join_view`s with random-access inner and outer iterators, meaning that the `__local_iterator` of `join_view`, if exists, must always be random-access. See: 

https://github.com/llvm/llvm-project/blob/d46458c11b4ea7906e1d02d598af2033df98f80e/libcxx/include/__ranges/join_view.h#L376-L383

So libcxx currently does not provide a segmented iterator with non-random access local iterators. However, I do believe that our SFINAE constraint for the basic overload of `for_each_n` should still consider the possibility of segmented iterators with non-random-access local iterators, as we may in the future extend `__segmented_iterator_traits` to some other iterator types. Moreover, I also added a test for `join_view` if `list<list<int>>`, which invokes the basic overload of `for_each_n`. 

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


More information about the libcxx-commits mailing list