[libcxx-commits] [libcxx] [libc++] Guard container benchmarks on library availability (PR #215408)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 10 18:50:19 PDT 2026
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/215408
>From 6ebd2c232a6b35712c5b03ef40eb37c39b4bbcae Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 10 Aug 2026 17:25:11 -0400
Subject: [PATCH] [libc++] Guard container benchmarks on library availability
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.
---
.../associative_container_benchmarks.h | 2 +-
.../sequence/sequence_container_benchmarks.h | 41 ++++++++++---------
.../benchmarks/containers/string.bench.cpp | 2 +-
3 files changed, 23 insertions(+), 22 deletions(-)
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;
More information about the libcxx-commits
mailing list