[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
Thu Apr 3 09:49:19 PDT 2025


================
@@ -33,16 +34,34 @@ int main(int argc, char** argv) {
 
             for ([[maybe_unused]] auto _ : st) {
               benchmark::DoNotOptimize(c);
-              auto result = for_each(first, last, [](int& x) { x = std::clamp(x, 10, 100); });
+              auto result = for_each(first, last, [](ElemType& x) { x = std::clamp<ElemType>(x, 10, 100); });
               benchmark::DoNotOptimize(result);
             }
           })
           ->Arg(8)
           ->Arg(32)
           ->Arg(50) // non power-of-two
+          ->Arg(1024)
+          ->Arg(4096)
           ->Arg(8192)
-          ->Arg(1 << 20);
+          ->Arg(1 << 14)
+          ->Arg(1 << 16)
+          ->Arg(1 << 18);
     };
+    bm.operator()<std::vector<char>>("std::for_each(vector<char>)", std_for_each);
+    bm.operator()<std::deque<char>>("std::for_each(deque<char>)", std_for_each);
+    bm.operator()<std::list<char>>("std::for_each(list<char>)", std_for_each);
+    bm.operator()<std::vector<char>>("rng::for_each(vector<char>)", std::ranges::for_each);
+    bm.operator()<std::deque<char>>("rng::for_each(deque<char>)", std::ranges::for_each);
+    bm.operator()<std::list<char>>("rng::for_each(list<char>)", std::ranges::for_each);
+
+    bm.operator()<std::vector<short>>("std::for_each(vector<short>)", std_for_each);
+    bm.operator()<std::deque<short>>("std::for_each(deque<short>)", std_for_each);
+    bm.operator()<std::list<short>>("std::for_each(list<short>)", std_for_each);
+    bm.operator()<std::vector<short>>("rng::for_each(vector<short>)", std::ranges::for_each);
+    bm.operator()<std::deque<short>>("rng::for_each(deque<short>)", std::ranges::for_each);
+    bm.operator()<std::list<short>>("rng::for_each(list<short>)", std::ranges::for_each);
----------------
winner245 wrote:

Thanks for the suggestion. I've removed all these benchmarks for `char` and `short`.

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


More information about the libcxx-commits mailing list