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

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 14 04:00:58 PST 2024


================
@@ -0,0 +1,435 @@
+// -*- 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> // for std::next
+#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();
----------------
philnik777 wrote:

We shouldn't use `PauseTiming` and `ResumeTiming` on single iteration benchmarks. These functions introduce significant overhead and significantly reduce signal due to that.

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


More information about the libcxx-commits mailing list