Skip to content

Conversation

philnik777
Copy link
Contributor

@philnik777 philnik777 commented Oct 21, 2025

Benchmark                                             89eef941c4ed    b96071c259fb    Difference    % Difference
--------------------------------------------------  --------------  --------------  ------------  --------------
rng::for_each(set<int>)/32                                   34.61           26.27         -8.34         -24.10%
rng::for_each(set<int>)/50                                   63.97           39.65        -24.32         -38.02%
rng::for_each(set<int>)/8                                     4.56            6.52          1.96          42.95%
rng::for_each(set<int>)/8192                              19102.12         8406.37     -10695.75         -55.99%
rng::for_each(set<int>::iterator)/32                         34.61           27.76         -6.85         -19.80%
rng::for_each(set<int>::iterator)/50                         63.98           41.98        -22.00         -34.38%
rng::for_each(set<int>::iterator)/8                           4.47            5.81          1.34          29.95%
rng::for_each(set<int>::iterator)/8192                    19055.30         8711.55     -10343.76         -54.28%

Copy link

github-actions bot commented Oct 21, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions ,h,cpp -- libcxx/include/__algorithm/specialized_algorithms.h libcxx/include/__algorithm/for_each.h libcxx/include/__algorithm/ranges_for_each.h libcxx/include/__tree libcxx/include/map libcxx/include/set libcxx/test/benchmarks/algorithms/nonmodifying/for_each.bench.cpp libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/libcxx/include/__algorithm/specialized_algorithms.h b/libcxx/include/__algorithm/specialized_algorithms.h
index 45078e2df..7cb0a2115 100644
--- a/libcxx/include/__algorithm/specialized_algorithms.h
+++ b/libcxx/include/__algorithm/specialized_algorithms.h
@@ -19,7 +19,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 // FIXME: This should really be an enum
 namespace _Algorithm {
-  struct __for_each {};
+struct __for_each {};
 } // namespace _Algorithm
 
 template <class, class>
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index d8e4a6da4..026cb00e1 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -1552,9 +1552,9 @@ struct __specialized_algorithm<_Algorithm::__for_each, __tree<_Tp, _Compare, _Al
   using __node_pointer _LIBCPP_NODEBUG = typename __tree<_Tp, _Compare, _Allocator>::__node_pointer;
 
   template <class _Func, class _Proj>
-#ifndef _LIBCPP_COMPILER_GCC
+#  ifndef _LIBCPP_COMPILER_GCC
   _LIBCPP_HIDE_FROM_ABI
-#endif
+#  endif
   static void __impl(__node_pointer __root, _Func& __func, _Proj& __proj) {
     if (__root->__left_)
       __impl(static_cast<__node_pointer>(__root->__left_), __func, __proj);
diff --git a/libcxx/include/map b/libcxx/include/map
index 99bda5702..3be31945c 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -1440,8 +1440,8 @@ struct __specialized_algorithm<_Algorithm::__for_each, map<_Key, _Tp, _Compare,
   // set's begin() and end() are identical with and without const qualifiaction
   template <class _Map, class _Func>
   _LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func) {
-    auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, typename __map::__base>()(
-        __map.__tree_, std::move(__func));
+    auto [_, __func2] =
+        __specialized_algorithm<_Algorithm::__for_each, typename __map::__base>()(__map.__tree_, std::move(__func));
     return std::make_pair(__map.end(), std::move(__func2));
   }
 };
@@ -2024,8 +2024,8 @@ struct __specialized_algorithm<_Algorithm::__for_each, multimap<_Key, _Tp, _Comp
   // set's begin() and end() are identical with and without const qualifiaction
   template <class _Map, class _Func>
   _LIBCPP_HIDE_FROM_ABI static auto operator()(_Map&& __map, _Func __func) {
-    auto [_, __func2] = __specialized_algorithm<_Algorithm::__for_each, typename __map::__base>()(
-        __map.__tree_, std::move(__func));
+    auto [_, __func2] =
+        __specialized_algorithm<_Algorithm::__for_each, typename __map::__base>()(__map.__tree_, std::move(__func));
     return std::make_pair(__map.end(), std::move(__func2));
   }
 };

@philnik777 philnik777 force-pushed the optimize_tree_iteration branch 2 times, most recently from b96071c to a31b2f2 Compare October 21, 2025 15:09
[libc++] Optimize std::for_each for __tree iterators
@philnik777 philnik777 force-pushed the optimize_tree_iteration branch from a31b2f2 to 8477cf3 Compare October 22, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant