[libcxx-commits] [libcxx] [libc++] Refactor the sequence container benchmarks (PR #119763)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 16 14:13:21 PST 2025


================
@@ -0,0 +1,510 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_BENCHMARKS_CONTAINERS_CONTAINER_BENCHMARKS_H
+#define TEST_BENCHMARKS_CONTAINERS_CONTAINER_BENCHMARKS_H
+
+#include <cstddef>
+#include <iterator>
+#include <ranges> // for std::from_range
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+namespace ContainerBenchmarks {
+
+template <class Container>
+void DoNotOptimizeData(Container& c) {
+  if constexpr (requires { c.data(); }) {
+    benchmark::DoNotOptimize(c.data());
+  } else {
+    benchmark::DoNotOptimize(&c);
+  }
+}
+
+//
+// Sequence container operations
+//
+template <class Container>
+void BM_ctor_size(benchmark::State& st) {
+  auto size = st.range(0);
+  char buffer[sizeof(Container)];
+  for (auto _ : st) {
+    std::construct_at(reinterpret_cast<Container*>(buffer), size);
+    benchmark::DoNotOptimize(buffer);
+    st.PauseTiming();
+    std::destroy_at(reinterpret_cast<Container*>(buffer));
+    st.ResumeTiming();
+  }
+}
+
+template <class Container>
+void BM_ctor_size_value(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const auto size = st.range(0);
+  ValueType value{};
+  benchmark::DoNotOptimize(value);
+  char buffer[sizeof(Container)];
+  for (auto _ : st) {
+    std::construct_at(reinterpret_cast<Container*>(buffer), size, value);
+    benchmark::DoNotOptimize(buffer);
+    st.PauseTiming();
+    std::destroy_at(reinterpret_cast<Container*>(buffer));
+    st.ResumeTiming();
+  }
+}
+
+template <class Container>
+void BM_ctor_iter_iter(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const auto size = st.range(0);
+  std::vector<ValueType> in(size);
+  const auto begin = in.begin();
+  const auto end   = in.end();
+  benchmark::DoNotOptimize(in);
+  char buffer[sizeof(Container)];
+  for (auto _ : st) {
+    std::construct_at(reinterpret_cast<Container*>(buffer), begin, end);
+    benchmark::DoNotOptimize(buffer);
+    st.PauseTiming();
+    std::destroy_at(reinterpret_cast<Container*>(buffer));
+    st.ResumeTiming();
+  }
+}
+
+#if TEST_STD_VER >= 23
+template <class Container>
+void BM_ctor_from_range(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const auto size = st.range(0);
+  std::vector<ValueType> in(size);
+  benchmark::DoNotOptimize(in);
+  char buffer[sizeof(Container)];
+  for (auto _ : st) {
+    std::construct_at(reinterpret_cast<Container*>(buffer), std::from_range, in);
+    benchmark::DoNotOptimize(buffer);
+    st.PauseTiming();
+    std::destroy_at(reinterpret_cast<Container*>(buffer));
+    st.ResumeTiming();
+  }
+}
+#endif
+
+template <class Container>
+void BM_ctor_copy(benchmark::State& st) {
+  auto size = st.range(0);
+  Container c(size);
+  char buffer[sizeof(Container)];
+  for (auto _ : st) {
+    std::construct_at(reinterpret_cast<Container*>(buffer), c);
+    benchmark::DoNotOptimize(buffer);
+    st.PauseTiming();
+    std::destroy_at(reinterpret_cast<Container*>(buffer));
+    st.ResumeTiming();
+  }
+}
+
+template <class Container>
+void BM_assignment(benchmark::State& st) {
+  auto size = st.range(0);
+  Container c1;
+  Container c2(size);
+  for (auto _ : st) {
+    c1 = c2;
+    DoNotOptimizeData(c1);
+    DoNotOptimizeData(c2);
+  }
+}
+
+template <typename Container>
+void BM_assign_inputiter(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  auto size       = st.range(0);
+  std::vector<ValueType> inputs(size);
+  Container c(inputs.begin(), inputs.end());
+  DoNotOptimizeData(c);
+  DoNotOptimizeData(inputs);
+  ValueType* first = inputs.data();
+  ValueType* last  = inputs.data() + inputs.size();
+
+  for (auto _ : st) {
+    c.assign(cpp17_input_iterator(first), cpp17_input_iterator(last));
+    benchmark::ClobberMemory();
+  }
+}
+
+template <class Container>
+void BM_insert_start(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const int count = st.range(0);
+  std::vector<ValueType> inputs(count);
+  Container c(inputs.begin(), inputs.end());
+  DoNotOptimizeData(c);
+
+  ValueType value{};
+  benchmark::DoNotOptimize(value);
+
+  for (auto _ : st) {
+    c.insert(c.begin(), value);
+    DoNotOptimizeData(c);
+
+    c.erase(std::prev(c.end())); // avoid growing indefinitely
+  }
+}
+
+template <class Container>
+  requires std::random_access_iterator<typename Container::iterator>
+void BM_insert_middle(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const int count = st.range(0);
+  std::vector<ValueType> inputs(count);
+  Container c(inputs.begin(), inputs.end());
+  DoNotOptimizeData(c);
+
+  ValueType value{};
+  benchmark::DoNotOptimize(value);
+
+  for (auto _ : st) {
+    auto mid = c.begin() + (count / 2); // requires random-access iterators in order to make sense
+    c.insert(mid, value);
+    DoNotOptimizeData(c);
+
+    c.erase(c.end() - 1); // avoid growing indefinitely
+  }
+}
+
+template <class Container>
+void BM_insert_input_iter_with_reserve_no_realloc(benchmark::State& st) {
+  using ValueType = typename Container::value_type;
+  const int count = st.range(0);
+  std::vector<ValueType> inputs(count);
+  const auto beg = cpp17_input_iterator(inputs.begin());
+  const auto end = cpp17_input_iterator(inputs.end());
+
+  auto size = 100; // arbitrary
+  Container c(size);
+  c.reserve(size + inputs.size()); // ensure no reallocation
+  for (auto _ : st) {
+    c.insert(c.begin(), beg, end);
+    DoNotOptimizeData(c);
+
+    st.PauseTiming();
+    c.erase(c.begin() + size, c.end()); // avoid growing indefinitely
+    st.ResumeTiming();
+  }
+}
+
+template <class Container>
+void BM_insert_input_iter_with_reserve_half_filled(benchmark::State& st) {
----------------
ldionne wrote:

Ah! Thanks a lot for the analysis, that makes sense. This got me thinking and I actually changed the benchmarks for insert back to something that, I think, should be closer to what you were originally trying to achieve. I think that makes more sense. I also fixed the benchmark to avoid destroying the whole container every time, which was probably screwing with the results.

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


More information about the libcxx-commits mailing list