[libcxx-commits] [libcxx] [libcxx] Optimize `ranges::fold_left_with_iter` for segmented iterators (PR #177853)
Connector Switch via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 4 07:17:24 PST 2026
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/177853
>From 50f1bd8d398bd8e24ff2f346ba538d38afde59cc Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 25 Jan 2026 20:37:09 +0800
Subject: [PATCH 1/2] optimize fold_left_with_iter
---
libcxx/docs/ReleaseNotes/23.rst | 4 ++++
libcxx/include/__algorithm/ranges_fold.h | 18 ++++++++++++------
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 73f5984768592..046ce4ad22b51 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -41,6 +41,10 @@ Implemented Papers
Improvements and New Features
-----------------------------
+- The ``std::ranges::fold_left_with_iter`` algorithm has been optimized for
+ segmented iterators, resulting in a performance improvement for
+ ``std::deque<int>`` iterators.
+
Deprecations and Removals
-------------------------
diff --git a/libcxx/include/__algorithm/ranges_fold.h b/libcxx/include/__algorithm/ranges_fold.h
index d2c3921398504..82614ec311164 100644
--- a/libcxx/include/__algorithm/ranges_fold.h
+++ b/libcxx/include/__algorithm/ranges_fold.h
@@ -10,12 +10,14 @@
#ifndef _LIBCPP___ALGORITHM_RANGES_FOLD_H
#define _LIBCPP___ALGORITHM_RANGES_FOLD_H
+#include <__algorithm/for_each.h>
#include <__concepts/assignable.h>
#include <__concepts/constructible.h>
#include <__concepts/convertible_to.h>
#include <__concepts/invocable.h>
#include <__concepts/movable.h>
#include <__config>
+#include <__functional/identity.h>
#include <__functional/invoke.h>
#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
@@ -80,18 +82,22 @@ concept __indirectly_binary_left_foldable =
struct __fold_left_with_iter {
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
- using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
+ using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
+ using __iter_ref = decltype(*__first);
if (__first == __last) {
return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))};
}
_Up __result = std::invoke(__f, std::move(__init), *__first);
- for (++__first; __first != __last; ++__first) {
- __result = std::invoke(__f, std::move(__result), *__first);
- }
-
- return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)};
+ ++__first;
+ auto __for_each_f = [&](__iter_ref __element) {
+ __result = std::invoke(__f, std::move(__result), std::forward<__iter_ref>(__element));
+ };
+ __identity __proj;
+ auto __end = std::__for_each(std::move(__first), std::move(__last), __for_each_f, __proj);
+
+ return fold_left_with_iter_result<_Ip, _Up>{std::move(__end), std::move(__result)};
}
template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
>From 8fb997795da2e9e00ae940f57b7851e5977c21e7 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 2 Feb 2026 23:20:55 +0800
Subject: [PATCH 2/2] mention improvement in release notes
---
libcxx/docs/ReleaseNotes/23.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 046ce4ad22b51..4a23ecf975aea 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -42,8 +42,8 @@ Improvements and New Features
-----------------------------
- The ``std::ranges::fold_left_with_iter`` algorithm has been optimized for
- segmented iterators, resulting in a performance improvement for
- ``std::deque<int>`` iterators.
+ segmented iterators, resulting in a performance improvement of up to 1.38x
+ for ``std::deque<int>`` iterators.
Deprecations and Removals
-------------------------
More information about the libcxx-commits
mailing list