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