[libcxx-commits] [libcxx] [libc++] Implement generic associative container benchmarks (PR #123663)
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Feb 3 10:51:27 PST 2025
================
@@ -0,0 +1,536 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_ASSOCIATIVE_CONTAINER_BENCHMARKS_H
+#define TEST_BENCHMARKS_CONTAINERS_ASSOCIATIVE_CONTAINER_BENCHMARKS_H
+
+#include <algorithm>
+#include <iterator>
+#include <map>
+#include <flat_map>
+#include <random>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+#include "../GenerateInput.h"
+#include "test_macros.h"
+
+namespace support {
+
+template <class Container>
+struct adapt_operations;
+
+template <class K>
+struct adapt_operations<std::set<K>> {
+ using ValueType = typename std::set<K>::value_type;
+ using KeyType = typename std::set<K>::key_type;
+ static ValueType value_from_key(KeyType const& k) { return k; }
+ static KeyType key_from_value(ValueType const& value) { return value; }
+};
+
+template <class K, class V>
+struct adapt_operations<std::map<K, V>> {
+ using ValueType = typename std::map<K, V>::value_type;
+ using KeyType = typename std::map<K, V>::key_type;
+ static ValueType value_from_key(KeyType const& k) { return {k, Generate<V>::arbitrary()}; }
+ static KeyType key_from_value(ValueType const& value) { return value.first; }
+};
+
+#if TEST_STD_VER >= 26
+template <class K, class V>
+struct adapt_operations<std::flat_map<K, V>> {
+ using ValueType = typename std::map<K, V>::value_type;
+ using KeyType = typename std::map<K, V>::key_type;
+ static ValueType value_from_key(KeyType const& k) { return {k, Generate<V>::arbitrary()}; }
+ static KeyType key_from_value(ValueType const& value) { return value.first; }
+};
+#endif
+
+template <class Container>
+void associative_container_benchmarks(std::string container) {
+ using Key = typename Container::key_type;
+ using Value = typename Container::value_type;
+
+ auto generate_unique_keys = [=](std::size_t n) {
+ std::set<Key> keys;
+ while (keys.size() < n) {
+ Key k = Generate<Key>::random();
+ keys.insert(k);
+ }
+ return std::vector<Key>(keys.begin(), keys.end());
+ };
+
+ auto add_dummy_mapped_type = [](std::vector<Key> const& keys) {
+ std::vector<Value> kv;
+ for (Key const& k : keys)
+ kv.push_back(adapt_operations<Container>::value_from_key(k));
+ return kv;
+ };
+
+ auto get_key = [](Value const& v) { return adapt_operations<Container>::key_from_value(v); };
+
+ // These benchmarks are structured to perform the operation being benchmarked
+ // a small number of times at each iteration, in order to offset the cost of
+ // PauseTiming() and ResumeTiming().
+ static constexpr std::size_t BatchSize = 10;
+
+ struct ScratchSpace {
+ char storage[sizeof(Container)];
----------------
mordante wrote:
We should make sure the storage is properly aligned.
https://github.com/llvm/llvm-project/pull/123663
More information about the libcxx-commits
mailing list