Bitcoin ABC  0.22.13
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  template <typename Stream, typename Operation>
25  inline void SerializationOp(Stream &s, Operation ser_action) {
26  READWRITE(x);
27  }
28 };
29 static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
30  "expected nontrivial_t to not be trivially constructible");
31 
32 typedef uint8_t trivial_t;
33 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
34  "expected trivial_t to be trivially constructible");
35 
36 template <typename T> static void PrevectorDestructor(benchmark::State &state) {
37  while (state.KeepRunning()) {
38  for (auto x = 0; x < 1000; ++x) {
41  t0.resize(28);
42  t1.resize(29);
43  }
44  }
45 }
46 
47 template <typename T> static void PrevectorClear(benchmark::State &state) {
48  while (state.KeepRunning()) {
49  for (auto x = 0; x < 1000; ++x) {
52  t0.resize(28);
53  t0.clear();
54  t1.resize(29);
55  t1.clear();
56  }
57  }
58 }
59 
60 template <typename T> static void PrevectorResize(benchmark::State &state) {
61  while (state.KeepRunning()) {
64  for (auto x = 0; x < 1000; ++x) {
65  t0.resize(28);
66  t0.resize(0);
67  t1.resize(29);
68  t1.resize(0);
69  }
70  }
71 }
72 
73 template <typename T>
75  CDataStream s0(SER_NETWORK, 0);
77  t0.resize(28);
78  for (auto x = 0; x < 900; ++x) {
79  s0 << t0;
80  }
81  t0.resize(100);
82  for (auto x = 0; x < 101; ++x) {
83  s0 << t0;
84  }
85  while (state.KeepRunning()) {
87  for (auto x = 0; x < 1000; ++x) {
88  s0 >> t1;
89  }
90  s0.Init(SER_NETWORK, 0);
91  }
92 }
93 
94 #define PREVECTOR_TEST(name, nontrivops, trivops) \
95  static void Prevector##name##Nontrivial(benchmark::State &state) { \
96  Prevector##name<nontrivial_t>(state); \
97  } \
98  BENCHMARK(Prevector##name##Nontrivial, nontrivops); \
99  static void Prevector##name##Trivial(benchmark::State &state) { \
100  Prevector##name<trivial_t>(state); \
101  } \
102  BENCHMARK(Prevector##name##Trivial, trivops);
103 
104 PREVECTOR_TEST(Clear, 28300, 88600)
105 PREVECTOR_TEST(Destructor, 28800, 88900)
106 PREVECTOR_TEST(Resize, 28900, 90300)
107 PREVECTOR_TEST(Deserialize, 6800, 52000)
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:253
void resize(size_type new_size)
Definition: prevector.h:414
static void PrevectorDestructor(benchmark::State &state)
Definition: prevector.cpp:36
void clear()
Definition: prevector.h:439
bool KeepRunning()
Definition: bench.h:71
static void PrevectorResize(benchmark::State &state)
Definition: prevector.cpp:60
uint8_t trivial_t
Definition: prevector.cpp:30
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
#define PREVECTOR_TEST(name, nontrivops, trivops)
Definition: prevector.cpp:94
static void PrevectorClear(benchmark::State &state)
Definition: prevector.cpp:47
ADD_SERIALIZE_METHODS
Definition: processor.cpp:264
static void PrevectorDeserialize(benchmark::State &state)
Definition: prevector.cpp:74
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
#define READWRITE(...)
Definition: serialize.h:191
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Definition: prevector.cpp:25