Bitcoin Core  31.0.0
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-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 <prevector.h>
6 
7 #include <bench/bench.h>
8 #include <script/script.h>
9 #include <serialize.h>
10 #include <streams.h>
11 
12 #include <type_traits>
13 #include <vector>
14 
16 {
17  int x{-1};
18  nontrivial_t() = default;
20 };
21 
22 static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
23  "expected nontrivial_t to not be trivially constructible");
24 
25 typedef unsigned char trivial_t;
26 static_assert(std::is_trivially_default_constructible_v<trivial_t>,
27  "expected trivial_t to be trivially constructible");
28 
29 template <typename T>
31 {
32  bench.batch(2).run([&] {
37  });
38 }
39 
40 template <typename T>
41 static void PrevectorClear(benchmark::Bench& bench)
42 {
45  bench.batch(2).run([&] {
47  t0.clear();
49  t1.clear();
50  });
51 }
52 
53 template <typename T>
54 static void PrevectorResize(benchmark::Bench& bench)
55 {
58  bench.batch(4).run([&] {
60  t0.resize(0);
62  t1.resize(0);
63  });
64 }
65 
66 template <typename T>
68 {
69  DataStream s0{};
72  for (auto x = 0; x < 900; ++x) {
73  s0 << t0;
74  }
75  t0.resize(100);
76  for (auto x = 0; x < 101; ++x) {
77  s0 << t0;
78  }
79  bench.batch(1000).run([&] {
81  for (auto x = 0; x < 1000; ++x) {
82  s0 >> t1;
83  }
84  s0.Rewind();
85  });
86 }
87 
88 template <typename T>
90 {
91  bench.run([&] {
92  std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
93  vec.reserve(260);
94  for (size_t i = 0; i < 260; ++i) {
95  vec.emplace_back();
96  }
97  });
98 }
99 
100 
101 template <typename T>
103 {
104  bench.run([&] {
105  std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
106  vec.reserve(260);
107  for (size_t i = 0; i < 260; ++i) {
108  // force allocation
109  vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
110  }
111  });
112 }
113 
114 #define PREVECTOR_TEST(name) \
115  static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
116  { \
117  Prevector##name<nontrivial_t>(bench); \
118  } \
119  BENCHMARK(Prevector##name##Nontrivial); \
120  static void Prevector##name##Trivial(benchmark::Bench& bench) \
121  { \
122  Prevector##name<trivial_t>(bench); \
123  } \
124  BENCHMARK(Prevector##name##Trivial);
125 
126 PREVECTOR_TEST(Clear)
127 PREVECTOR_TEST(Destructor)
128 PREVECTOR_TEST(Resize)
129 PREVECTOR_TEST(Deserialize)
130 PREVECTOR_TEST(FillVectorDirect)
131 PREVECTOR_TEST(FillVectorIndirect)
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:67
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:54
void resize(size_type new_size)
Definition: prevector.h:276
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:30
void clear()
Definition: prevector.h:303
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:41
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:89
nontrivial_t()=default
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:41
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:19
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
unsigned char trivial_t
Definition: prevector.cpp:23
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:114
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1258
#define READWRITE(...)
Definition: serialize.h:145
#define T(expected, seed, data)
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:102