[libcxx-commits] [libcxx] [libc++] Add benchmarks for copy algorithms (PR #127328)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 18 14:07:02 PST 2025
================
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <deque>
+#include <iterator>
+#include <list>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../../GenerateInput.h"
+#include "test_macros.h"
+
+template <class Container, class Operation>
+void bm_general(std::string operation_name, Operation copy) {
+ auto bench = [copy](auto& st) {
+ auto const size = st.range(0);
+ using ValueType = typename Container::value_type;
+ Container c;
+ std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
+
+ std::vector<ValueType> out(size);
+
+ for ([[maybe_unused]] auto _ : st) {
+ auto result = copy(c.begin(), c.end(), out.begin());
+ benchmark::DoNotOptimize(result);
+ benchmark::DoNotOptimize(out);
+ benchmark::DoNotOptimize(c);
+ benchmark::ClobberMemory();
----------------
ldionne wrote:
Based on https://github.com/google/benchmark/blob/main/docs/user_guide.md#preventing-optimization, my understanding is that you first escape variables you want the compiler to "lose track of" to prevent optimization, and you then use `ClobberMemory` to simulate a write to all of memory. IIUC, this causes the compiler to assume that every escaped variable might have been written to, meaning it's not able to do something like hoist the value out of the loop, etc.
I've read all the documentation I could find on these functions and that's my understanding, but it's entirely possible that it's not correct. How would you have written this benchmark and why?
https://github.com/llvm/llvm-project/pull/127328
More information about the libcxx-commits
mailing list