[libc++] Add more benchmarks for the associative container hint interfaces (#162840)

The current benchmarks test a very specific case, which makes them
rather misleading. This adds new benchmarks so we have better coverage.
This commit is contained in:
Nikolas Klauser
2025-10-13 10:50:58 +02:00
committed by GitHub
parent e90f8d84b0
commit cdc8e8d092

View File

@@ -305,11 +305,19 @@ void associative_container_benchmarks(std::string container) {
// The insert(hint, ...) methods are only relevant for ordered containers, and we lack
// a good way to compute a hint for unordered ones.
if constexpr (is_ordered_container) {
bench("insert(hint, value) (good hint)", [=](auto& st) {
auto insert_good_hint_bench = [=](bool bench_end_iter, auto& st) {
const std::size_t size = st.range(0);
std::vector<Value> in = make_value_types(generate_unique_keys(size + 1));
Value to_insert = in.back();
in.pop_back();
auto skipped_val = bench_end_iter ? in.size() - 1 : in.size() / 2;
Value to_insert = in[skipped_val];
{ // Remove the element
std::vector<Value> tmp;
tmp.reserve(in.size() - 1);
for (size_t i = 0; i != in.size(); ++i)
if (i != skipped_val)
tmp.emplace_back(in[i]);
in = std::move(tmp);
}
std::vector<Container> c(BatchSize, Container(in.begin(), in.end()));
typename Container::iterator hints[BatchSize];
@@ -332,13 +340,23 @@ void associative_container_benchmarks(std::string container) {
}
st.ResumeTiming();
}
});
};
bench("insert(hint, value) (good hint, end)", [=](auto& state) { insert_good_hint_bench(true, state); });
bench("insert(hint, value) (good hint, middle)", [=](auto& state) { insert_good_hint_bench(false, state); });
bench("insert(hint, value) (bad hint)", [=](auto& st) {
auto insert_bad_hint_bench = [=](bool bench_end_iter, auto& st) {
const std::size_t size = st.range(0);
std::vector<Value> in = make_value_types(generate_unique_keys(size + 1));
Value to_insert = in.back();
in.pop_back();
auto skipped_val = bench_end_iter ? in.size() - 1 : in.size() / 2;
Value to_insert = in[skipped_val];
{ // Remove the element
std::vector<Value> tmp;
tmp.reserve(in.size() - 1);
for (size_t i = 0; i != in.size(); ++i)
if (i != skipped_val)
tmp.emplace_back(in[i]);
in = std::move(tmp);
}
std::vector<Container> c(BatchSize, Container(in.begin(), in.end()));
while (st.KeepRunningBatch(BatchSize)) {
@@ -355,7 +373,10 @@ void associative_container_benchmarks(std::string container) {
}
st.ResumeTiming();
}
});
};
bench("insert(hint, value) (bad hint, end)", [=](auto& state) { insert_bad_hint_bench(true, state); });
bench("insert(hint, value) (bad hint, middle)", [=](auto& state) { insert_bad_hint_bench(false, state); });
}
bench("insert(iterator, iterator) (all new keys)", [=](auto& st) {