1*c303c47eSjoerg // -*- C++ -*-
2*c303c47eSjoerg //===----------------------------------------------------------------------===//
3*c303c47eSjoerg //
4*c303c47eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*c303c47eSjoerg // See https://llvm.org/LICENSE.txt for license information.
6*c303c47eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*c303c47eSjoerg //
8*c303c47eSjoerg //===----------------------------------------------------------------------===//
9*c303c47eSjoerg 
10*c303c47eSjoerg #ifndef BENCHMARK_CONTAINER_BENCHMARKS_H
11*c303c47eSjoerg #define BENCHMARK_CONTAINER_BENCHMARKS_H
12*c303c47eSjoerg 
13*c303c47eSjoerg #include <cassert>
14*c303c47eSjoerg 
15*c303c47eSjoerg #include "Utilities.h"
16*c303c47eSjoerg #include "benchmark/benchmark.h"
17*c303c47eSjoerg 
18*c303c47eSjoerg namespace ContainerBenchmarks {
19*c303c47eSjoerg 
20*c303c47eSjoerg template <class Container>
BM_ConstructSize(benchmark::State & st,Container)21*c303c47eSjoerg void BM_ConstructSize(benchmark::State& st, Container) {
22*c303c47eSjoerg   auto size = st.range(0);
23*c303c47eSjoerg   for (auto _ : st) {
24*c303c47eSjoerg     Container c(size);
25*c303c47eSjoerg     DoNotOptimizeData(c);
26*c303c47eSjoerg   }
27*c303c47eSjoerg }
28*c303c47eSjoerg 
29*c303c47eSjoerg template <class Container>
BM_ConstructSizeValue(benchmark::State & st,Container,typename Container::value_type const & val)30*c303c47eSjoerg void BM_ConstructSizeValue(benchmark::State& st, Container, typename Container::value_type const& val) {
31*c303c47eSjoerg   const auto size = st.range(0);
32*c303c47eSjoerg   for (auto _ : st) {
33*c303c47eSjoerg     Container c(size, val);
34*c303c47eSjoerg     DoNotOptimizeData(c);
35*c303c47eSjoerg   }
36*c303c47eSjoerg }
37*c303c47eSjoerg 
38*c303c47eSjoerg template <class Container, class GenInputs>
BM_ConstructIterIter(benchmark::State & st,Container,GenInputs gen)39*c303c47eSjoerg void BM_ConstructIterIter(benchmark::State& st, Container, GenInputs gen) {
40*c303c47eSjoerg     auto in = gen(st.range(0));
41*c303c47eSjoerg     const auto begin = in.begin();
42*c303c47eSjoerg     const auto end = in.end();
43*c303c47eSjoerg     benchmark::DoNotOptimize(&in);
44*c303c47eSjoerg     while (st.KeepRunning()) {
45*c303c47eSjoerg         Container c(begin, end);
46*c303c47eSjoerg         DoNotOptimizeData(c);
47*c303c47eSjoerg     }
48*c303c47eSjoerg }
49*c303c47eSjoerg 
50*c303c47eSjoerg template <class Container, class GenInputs>
BM_InsertValue(benchmark::State & st,Container c,GenInputs gen)51*c303c47eSjoerg void BM_InsertValue(benchmark::State& st, Container c, GenInputs gen) {
52*c303c47eSjoerg     auto in = gen(st.range(0));
53*c303c47eSjoerg     const auto end = in.end();
54*c303c47eSjoerg     while (st.KeepRunning()) {
55*c303c47eSjoerg         c.clear();
56*c303c47eSjoerg         for (auto it = in.begin(); it != end; ++it) {
57*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.insert(*it).first));
58*c303c47eSjoerg         }
59*c303c47eSjoerg         benchmark::ClobberMemory();
60*c303c47eSjoerg     }
61*c303c47eSjoerg }
62*c303c47eSjoerg 
63*c303c47eSjoerg template <class Container, class GenInputs>
BM_InsertValueRehash(benchmark::State & st,Container c,GenInputs gen)64*c303c47eSjoerg void BM_InsertValueRehash(benchmark::State& st, Container c, GenInputs gen) {
65*c303c47eSjoerg     auto in = gen(st.range(0));
66*c303c47eSjoerg     const auto end = in.end();
67*c303c47eSjoerg     while (st.KeepRunning()) {
68*c303c47eSjoerg         c.clear();
69*c303c47eSjoerg         c.rehash(16);
70*c303c47eSjoerg         for (auto it = in.begin(); it != end; ++it) {
71*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.insert(*it).first));
72*c303c47eSjoerg         }
73*c303c47eSjoerg         benchmark::ClobberMemory();
74*c303c47eSjoerg     }
75*c303c47eSjoerg }
76*c303c47eSjoerg 
77*c303c47eSjoerg 
78*c303c47eSjoerg template <class Container, class GenInputs>
BM_InsertDuplicate(benchmark::State & st,Container c,GenInputs gen)79*c303c47eSjoerg void BM_InsertDuplicate(benchmark::State& st, Container c, GenInputs gen) {
80*c303c47eSjoerg     auto in = gen(st.range(0));
81*c303c47eSjoerg     const auto end = in.end();
82*c303c47eSjoerg     c.insert(in.begin(), in.end());
83*c303c47eSjoerg     benchmark::DoNotOptimize(&c);
84*c303c47eSjoerg     benchmark::DoNotOptimize(&in);
85*c303c47eSjoerg     while (st.KeepRunning()) {
86*c303c47eSjoerg         for (auto it = in.begin(); it != end; ++it) {
87*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.insert(*it).first));
88*c303c47eSjoerg         }
89*c303c47eSjoerg         benchmark::ClobberMemory();
90*c303c47eSjoerg     }
91*c303c47eSjoerg }
92*c303c47eSjoerg 
93*c303c47eSjoerg 
94*c303c47eSjoerg template <class Container, class GenInputs>
BM_EmplaceDuplicate(benchmark::State & st,Container c,GenInputs gen)95*c303c47eSjoerg void BM_EmplaceDuplicate(benchmark::State& st, Container c, GenInputs gen) {
96*c303c47eSjoerg     auto in = gen(st.range(0));
97*c303c47eSjoerg     const auto end = in.end();
98*c303c47eSjoerg     c.insert(in.begin(), in.end());
99*c303c47eSjoerg     benchmark::DoNotOptimize(&c);
100*c303c47eSjoerg     benchmark::DoNotOptimize(&in);
101*c303c47eSjoerg     while (st.KeepRunning()) {
102*c303c47eSjoerg         for (auto it = in.begin(); it != end; ++it) {
103*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.emplace(*it).first));
104*c303c47eSjoerg         }
105*c303c47eSjoerg         benchmark::ClobberMemory();
106*c303c47eSjoerg     }
107*c303c47eSjoerg }
108*c303c47eSjoerg 
109*c303c47eSjoerg template <class Container, class GenInputs>
BM_Find(benchmark::State & st,Container c,GenInputs gen)110*c303c47eSjoerg static void BM_Find(benchmark::State& st, Container c, GenInputs gen) {
111*c303c47eSjoerg     auto in = gen(st.range(0));
112*c303c47eSjoerg     c.insert(in.begin(), in.end());
113*c303c47eSjoerg     benchmark::DoNotOptimize(&(*c.begin()));
114*c303c47eSjoerg     const auto end = in.data() + in.size();
115*c303c47eSjoerg     while (st.KeepRunning()) {
116*c303c47eSjoerg         for (auto it = in.data(); it != end; ++it) {
117*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.find(*it)));
118*c303c47eSjoerg         }
119*c303c47eSjoerg         benchmark::ClobberMemory();
120*c303c47eSjoerg     }
121*c303c47eSjoerg }
122*c303c47eSjoerg 
123*c303c47eSjoerg template <class Container, class GenInputs>
BM_FindRehash(benchmark::State & st,Container c,GenInputs gen)124*c303c47eSjoerg static void BM_FindRehash(benchmark::State& st, Container c, GenInputs gen) {
125*c303c47eSjoerg     c.rehash(8);
126*c303c47eSjoerg     auto in = gen(st.range(0));
127*c303c47eSjoerg     c.insert(in.begin(), in.end());
128*c303c47eSjoerg     benchmark::DoNotOptimize(&(*c.begin()));
129*c303c47eSjoerg     const auto end = in.data() + in.size();
130*c303c47eSjoerg     while (st.KeepRunning()) {
131*c303c47eSjoerg         for (auto it = in.data(); it != end; ++it) {
132*c303c47eSjoerg             benchmark::DoNotOptimize(&(*c.find(*it)));
133*c303c47eSjoerg         }
134*c303c47eSjoerg         benchmark::ClobberMemory();
135*c303c47eSjoerg     }
136*c303c47eSjoerg }
137*c303c47eSjoerg 
138*c303c47eSjoerg } // end namespace ContainerBenchmarks
139*c303c47eSjoerg 
140*c303c47eSjoerg #endif // BENCHMARK_CONTAINER_BENCHMARKS_H
141