1 // Copyright (c) 2022-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 5 #include <bench/bench.h>
6 #include <support/allocators/pool.h>
7 8 #include <cstddef>
9 #include <cstdint>
10 #include <functional>
11 #include <unordered_map>
12 #include <utility>
13 14 template <typename Map>
15 void BenchFillClearMap(benchmark::Bench& bench, Map& map)
16 {
17 size_t batch_size = 5000;
18 19 // make sure each iteration of the benchmark contains exactly 5000 inserts and one clear.
20 // do this at least 10 times so we get reasonable accurate results
21 22 bench.batch(batch_size).minEpochIterations(10).run([&] {
23 auto rng = ankerl::nanobench::Rng(1234);
24 for (size_t i = 0; i < batch_size; ++i) {
25 map[rng()];
26 }
27 map.clear();
28 });
29 }
30 31 static void PoolAllocator_StdUnorderedMap(benchmark::Bench& bench)
32 {
33 auto map = std::unordered_map<uint64_t, uint64_t>();
34 BenchFillClearMap(bench, map);
35 }
36 37 static void PoolAllocator_StdUnorderedMapWithPoolResource(benchmark::Bench& bench)
38 {
39 using Map = std::unordered_map<uint64_t,
40 uint64_t,
41 std::hash<uint64_t>,
42 std::equal_to<uint64_t>,
43 PoolAllocator<std::pair<const uint64_t, uint64_t>,
44 sizeof(std::pair<const uint64_t, uint64_t>) + 4 * sizeof(void*)>>;
45 46 // make sure the resource supports large enough pools to hold the node. We do this by adding the size of a few pointers to it.
47 auto pool_resource = Map::allocator_type::ResourceType();
48 auto map = Map{0, std::hash<uint64_t>{}, std::equal_to<uint64_t>{}, &pool_resource};
49 BenchFillClearMap(bench, map);
50 }
51 52 BENCHMARK(PoolAllocator_StdUnorderedMap);
53 BENCHMARK(PoolAllocator_StdUnorderedMapWithPoolResource);
54