Bitcoin Core  0.20.99
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018 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 // GCC 4.8 is missing some C++11 type_traits,
13 // https://www.gnu.org/software/gcc/gcc-5/changes.html
14 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
15 #define IS_TRIVIALLY_CONSTRUCTIBLE std::has_trivial_default_constructor
16 #else
17 #define IS_TRIVIALLY_CONSTRUCTIBLE std::is_trivially_default_constructible
18 #endif
19 
20 struct nontrivial_t {
21  int x;
22  nontrivial_t() :x(-1) {}
24 };
25 static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
26  "expected nontrivial_t to not be trivially constructible");
27 
28 typedef unsigned char trivial_t;
29 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
30  "expected trivial_t to be trivially constructible");
31 
32 template <typename T>
34 {
35  while (state.KeepRunning()) {
36  for (auto x = 0; x < 1000; ++x) {
39  t0.resize(28);
40  t1.resize(29);
41  }
42  }
43 }
44 
45 template <typename T>
46 static void PrevectorClear(benchmark::State& state)
47 {
48 
49  while (state.KeepRunning()) {
50  for (auto x = 0; x < 1000; ++x) {
53  t0.resize(28);
54  t0.clear();
55  t1.resize(29);
56  t1.clear();
57  }
58  }
59 }
60 
61 template <typename T>
62 static void PrevectorResize(benchmark::State& state)
63 {
64  while (state.KeepRunning()) {
67  for (auto x = 0; x < 1000; ++x) {
68  t0.resize(28);
69  t0.resize(0);
70  t1.resize(29);
71  t1.resize(0);
72  }
73  }
74 }
75 
76 template <typename T>
78 {
79  CDataStream s0(SER_NETWORK, 0);
81  t0.resize(28);
82  for (auto x = 0; x < 900; ++x) {
83  s0 << t0;
84  }
85  t0.resize(100);
86  for (auto x = 0; x < 101; ++x) {
87  s0 << t0;
88  }
89  while (state.KeepRunning()) {
91  for (auto x = 0; x < 1000; ++x) {
92  s0 >> t1;
93  }
94  s0.Init(SER_NETWORK, 0);
95  }
96 }
97 
98 #define PREVECTOR_TEST(name, nontrivops, trivops) \
99  static void Prevector ## name ## Nontrivial(benchmark::State& state) { \
100  Prevector ## name<nontrivial_t>(state); \
101  } \
102  BENCHMARK(Prevector ## name ## Nontrivial, nontrivops); \
103  static void Prevector ## name ## Trivial(benchmark::State& state) { \
104  Prevector ## name<trivial_t>(state); \
105  } \
106  BENCHMARK(Prevector ## name ## Trivial, trivops);
107 
108 PREVECTOR_TEST(Clear, 28300, 88600)
109 PREVECTOR_TEST(Destructor, 28800, 88900)
110 PREVECTOR_TEST(Resize, 28900, 90300)
111 PREVECTOR_TEST(Deserialize, 6800, 52000)
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:259
void resize(size_type new_size)
Definition: prevector.h:316
static void PrevectorDestructor(benchmark::State &state)
Definition: prevector.cpp:33
void clear()
Definition: prevector.h:343
bool KeepRunning()
Definition: bench.h:69
static void PrevectorResize(benchmark::State &state)
Definition: prevector.cpp:62
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
#define PREVECTOR_TEST(name, nontrivops, trivops)
Definition: prevector.cpp:98
static void PrevectorClear(benchmark::State &state)
Definition: prevector.cpp:46
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:23
unsigned char trivial_t
Definition: prevector.cpp:26
static void PrevectorDeserialize(benchmark::State &state)
Definition: prevector.cpp:77
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
#define READWRITE(...)
Definition: serialize.h:171