[libcxx-commits] [libcxx] [libc++] Optimize {std, ranges}::for_each for iterating over __trees (PR #164405)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Dec 10 07:58:27 PST 2025
================
@@ -52,6 +55,66 @@ int main(int argc, char** argv) {
bm.operator()<std::list<int>>("rng::for_each(list<int>)", std::ranges::for_each);
}
+ // std::{,range::}for_each for associative containers
+ {
+ auto iterator_bm = []<class Container, bool IsMapLike>(
+ std::type_identity<Container>, std::bool_constant<IsMapLike>, std::string name) {
+ benchmark::RegisterBenchmark(
+ name,
+ [](auto& st) {
+ Container c;
+ for (std::int64_t i = 0; i != st.range(0); ++i) {
+ if constexpr (IsMapLike)
+ c.emplace(i, i);
+ else
+ c.emplace(i);
+ }
+
+ for (auto _ : st) {
+ benchmark::DoNotOptimize(c);
+ std::for_each(c.begin(), c.end(), [](auto v) { benchmark::DoNotOptimize(&v); });
+ }
+ })
+ ->Arg(8)
+ ->Arg(32)
+ ->Arg(50) // non power-of-two
+ ->Arg(8192);
+ };
+ iterator_bm(std::type_identity<std::set<int>>{}, std::false_type{}, "rng::for_each(set<int>::iterator)");
+ iterator_bm(std::type_identity<std::multiset<int>>{}, std::false_type{}, "rng::for_each(multiset<int>::iterator)");
+ iterator_bm(std::type_identity<std::map<int, int>>{}, std::true_type{}, "rng::for_each(map<int>::iterator)");
+ iterator_bm(
+ std::type_identity<std::multimap<int, int>>{}, std::true_type{}, "rng::for_each(multimap<int>::iterator)");
+
+ auto container_bm = []<class Container, bool IsMapLike>(
+ std::type_identity<Container>, std::bool_constant<IsMapLike>, std::string name) {
+ benchmark::RegisterBenchmark(
+ name,
+ [](auto& st) {
+ Container c;
+ for (std::int64_t i = 0; i != st.range(0); ++i) {
----------------
ldionne wrote:
If we use `std::size_t const size = st.range(0);`, Clang won't complain anymore and we can be consistent again.
https://github.com/llvm/llvm-project/pull/164405
More information about the libcxx-commits
mailing list