[libcxx-commits] [libcxx] [libc++] Guard container benchmarks on library availability (PR #215408)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 10 15:02:22 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

<details>
<summary>Changes</summary>

Instead of using TEST_STD_VER, use FTMs or requires clauses to enable benchmarks for some recent features like `append_range`. This is needed since older versions of the library don't provide these features, so the container benchmarks as a whole would fail to compile instead of just a few methods being disabled.

---
Full diff: https://github.com/llvm/llvm-project/pull/215408.diff


3 Files Affected:

- (modified) libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h (+1-1) 
- (modified) libcxx/test/benchmarks/containers/sequence/sequence_container_benchmarks.h (+21-20) 
- (modified) libcxx/test/benchmarks/containers/string.bench.cpp (+1-1) 


``````````diff
diff --git a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
index 82ceefa0459ef..72fd0ac9bfa0e 100644
--- a/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
+++ b/libcxx/test/benchmarks/containers/associative/associative_container_benchmarks.h
@@ -547,7 +547,7 @@ void associative_container_benchmarks(std::string container) {
       }
     });
 
-#if TEST_STD_VER >= 23
+#if defined(__cpp_lib_ranges_zip) && __cpp_lib_ranges_zip >= 202110L
     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));
diff --git a/libcxx/test/benchmarks/containers/sequence/sequence_container_benchmarks.h b/libcxx/test/benchmarks/containers/sequence/sequence_container_benchmarks.h
index 3ddeb7571f2d4..81b71519a761c 100644
--- a/libcxx/test/benchmarks/containers/sequence/sequence_container_benchmarks.h
+++ b/libcxx/test/benchmarks/containers/sequence/sequence_container_benchmarks.h
@@ -312,9 +312,10 @@ void sequence_container_benchmarks(std::string container) {
   /////////////////////////
   // Appending elements
   /////////////////////////
-  static constexpr bool has_push_back = requires(Container c, ValueType v) { c.push_back(v); };
-  static constexpr bool has_capacity  = requires(Container c) { c.capacity(); };
-  static constexpr bool has_reserve   = requires(Container c) { c.reserve(0); };
+  static constexpr bool has_push_back    = requires(Container c, ValueType v) { c.push_back(v); };
+  static constexpr bool has_capacity     = requires(Container c) { c.capacity(); };
+  static constexpr bool has_reserve      = requires(Container c) { c.reserve(0); };
+  static constexpr bool has_append_range = requires(Container c, std::vector<ValueType> v) { c.append_range(v); };
   if constexpr (has_push_back) {
     if constexpr (has_capacity) {
       // For containers where we can observe capacity(), push_back a single element
@@ -398,26 +399,26 @@ void sequence_container_benchmarks(std::string container) {
         }
       });
 
-#if TEST_STD_VER >= 23
-    for (auto gen : generators)
-      bench("append_range() (into empty container)" + tostr(gen), [gen](auto& state) {
-        auto const size = state.range(0);
-        std::vector<ValueType> in;
-        std::generate_n(std::back_inserter(in), size, gen);
-        DoNotOptimizeData(in);
+    if constexpr (has_append_range) {
+      for (auto gen : generators)
+        bench("append_range() (into empty container)" + tostr(gen), [gen](auto& state) {
+          auto const size = state.range(0);
+          std::vector<ValueType> in;
+          std::generate_n(std::back_inserter(in), size, gen);
+          DoNotOptimizeData(in);
 
-        Container c;
-        DoNotOptimizeData(c);
-        for (auto _ : state) {
-          c.append_range(in);
+          Container c;
           DoNotOptimizeData(c);
+          for (auto _ : state) {
+            c.append_range(in);
+            DoNotOptimizeData(c);
 
-          state.PauseTiming();
-          c.clear();
-          state.ResumeTiming();
-        }
-      });
-#endif
+            state.PauseTiming();
+            c.clear();
+            state.ResumeTiming();
+          }
+        });
+    }
   }
 
   /////////////////////////
diff --git a/libcxx/test/benchmarks/containers/string.bench.cpp b/libcxx/test/benchmarks/containers/string.bench.cpp
index 26e2f62ad57fe..49f688aedae5c 100644
--- a/libcxx/test/benchmarks/containers/string.bench.cpp
+++ b/libcxx/test/benchmarks/containers/string.bench.cpp
@@ -205,7 +205,7 @@ int main(int argc, char** argv) {
     }
   });
 
-#if TEST_STD_VER >= 23
+#if defined(__cpp_lib_string_resize_and_overwrite) && __cpp_lib_string_resize_and_overwrite >= 202110L
   bench("std::basic_string::resize_and_overwrite()",
         []<class CharT>(std::type_identity<CharT>, benchmark::State& state) {
           std::basic_string<CharT> str;

``````````

</details>


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


More information about the libcxx-commits mailing list