[libcxx-commits] [libcxx] [libc++] Trim down associative container benchmark sizes (PR #212252)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 30 08:56:13 PDT 2026


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/212252

>From cdd52001e3f72629b45e5ef79ddc16ada572beb4 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Sat, 25 Jul 2026 07:36:01 -0400
Subject: [PATCH 1/3] [libc++] Reduce associative container benchmark size
 ladder

Based on local measurements, the associative containers are by far the
slowest benchmarks to run. While there is value in benchmarking small
and large sizes, we can significantly cut down benchmark times by stripping
down to only 2 sizes (from 4).

With this patch, the benchmarks go from 3124s to 1766s across the
associative container benchmarks, which is -43% of their execution
time, and corresponds to ~15% of the entire benchmark suite.

Towards #173032
---
 .../containers/associative/associative_container_benchmarks.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
index a55f187d2ac80..af403553c657d 100644
--- a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
+++ b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
@@ -63,10 +63,10 @@ void associative_container_benchmarks(std::string container) {
   auto get_key = [](Value const& v) { return adapt_operations<Container>::key_from_value(v); };
 
   auto bench = [&](std::string operation, auto f) {
-    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(0)->Arg(32)->Arg(1024)->Arg(8192);
+    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(8192);
   };
   auto bench_non_empty = [&](std::string operation, auto f) {
-    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(1024)->Arg(8192);
+    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(8192);
   };
 
   static constexpr bool is_multi_key_container =

>From 41f608b8eec48c6fb01d8e92073bff2deebfc76b Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 30 Jul 2026 11:37:02 -0400
Subject: [PATCH 2/3] Re-add 0 size to benchmarks we want to run on empty
 containers

---
 .../containers/associative/associative_container_benchmarks.h   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
index af403553c657d..677c921790a6f 100644
--- a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
+++ b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
@@ -63,7 +63,7 @@ void associative_container_benchmarks(std::string container) {
   auto get_key = [](Value const& v) { return adapt_operations<Container>::key_from_value(v); };
 
   auto bench = [&](std::string operation, auto f) {
-    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(8192);
+    benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(0)->Arg(32)->Arg(8192);
   };
   auto bench_non_empty = [&](std::string operation, auto f) {
     benchmark::RegisterBenchmark(container + "::" + operation, f)->Arg(32)->Arg(8192);

>From 7e7678612146b228f52fb06dab8445912f820f73 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 30 Jul 2026 11:55:58 -0400
Subject: [PATCH 3/3] Switch more benchmarks to non-empty

---
 .../associative_container_benchmarks.h        | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
index 677c921790a6f..2cfbfdc4728e1 100644
--- a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
+++ b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
@@ -131,7 +131,7 @@ void associative_container_benchmarks(std::string container) {
     }
   });
 
-  bench("ctor(Self&&, const allocator_type&) (different allocs)", [=](auto& st) {
+  bench_non_empty("ctor(Self&&, const allocator_type&) (different allocs)", [=](auto& st) {
     using PMRContainer = adapt_operations<Container>::template rebind_alloc<
         std::pmr::polymorphic_allocator<typename Container::value_type>>;
 
@@ -493,11 +493,14 @@ void associative_container_benchmarks(std::string container) {
       st.ResumeTiming();
     }
   };
-  bench("insert(iterator, iterator) (all new keys, end)", [&](auto& state) { insert_iter_iter_bench(true, state); });
-  bench("insert(iterator, iterator) (all new keys, middle)",
-        [&](auto& state) { insert_iter_iter_bench(false, state); });
+  bench_non_empty("insert(iterator, iterator) (all new keys, end)", [&](auto& state) {
+    insert_iter_iter_bench(true, state);
+  });
+  bench_non_empty("insert(iterator, iterator) (all new keys, middle)", [&](auto& state) {
+    insert_iter_iter_bench(false, state);
+  });
 
-  bench("insert(iterator, iterator) (half new keys)", [=](auto& st) {
+  bench_non_empty("insert(iterator, iterator) (half new keys)", [=](auto& st) {
     const std::size_t size = st.range(0);
     std::vector<Value> in  = make_value_types(generate_unique_keys(size));
 
@@ -521,7 +524,7 @@ void associative_container_benchmarks(std::string container) {
   });
 
   if constexpr (is_map_like) {
-    bench("insert(iterator, iterator) (product_iterator from same type)", [=](auto& st) {
+    bench_non_empty("insert(iterator, iterator) (product_iterator from same type)", [=](auto& st) {
       const std::size_t size = st.range(0);
       std::vector<Value> in  = make_value_types(generate_unique_keys(size + (size / 10)));
       Container source(in.begin(), in.end());
@@ -540,7 +543,7 @@ void associative_container_benchmarks(std::string container) {
     });
 
 #if TEST_STD_VER >= 23
-    bench("insert(iterator, iterator) (product_iterator from zip_view)", [=](auto& st) {
+    bench_non_empty("insert(iterator, iterator) (product_iterator from zip_view)", [=](auto& st) {
       const std::size_t size = st.range(0);
       std::vector<Key> keys  = generate_unique_keys(size + (size / 10));
       std::sort(keys.begin(), keys.end());
@@ -647,7 +650,7 @@ void associative_container_benchmarks(std::string container) {
     }
   });
 
-  bench("erase(iterator, iterator) (erase half the container)", [=](auto& st) {
+  bench_non_empty("erase(iterator, iterator) (erase half the container)", [=](auto& st) {
     const std::size_t size = st.range(0);
     std::vector<Value> in  = make_value_types(generate_unique_keys(size));
     Container c(in.begin(), in.end());



More information about the libcxx-commits mailing list