Bitcoin Core  0.19.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  template <typename Stream, typename Operation>
25  inline void SerializationOp(Stream& s, Operation ser_action) {READWRITE(x);}
26 };
27 static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
28  "expected nontrivial_t to not be trivially constructible");
29 
30 typedef unsigned char trivial_t;
31 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
32  "expected trivial_t to be trivially constructible");
33 
34 template <typename T>
36 {
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>
48 static void PrevectorClear(benchmark::State& state)
49 {
50 
51  while (state.KeepRunning()) {
52  for (auto x = 0; x < 1000; ++x) {
55  t0.resize(28);
56  t0.clear();
57  t1.resize(29);
58  t1.clear();
59  }
60  }
61 }
62 
63 template <typename T>
64 static void PrevectorResize(benchmark::State& state)
65 {
66  while (state.KeepRunning()) {
69  for (auto x = 0; x < 1000; ++x) {
70  t0.resize(28);
71  t0.resize(0);
72  t1.resize(29);
73  t1.resize(0);
74  }
75  }
76 }
77 
78 template <typename T>
80 {
81  CDataStream s0(SER_NETWORK, 0);
83  t0.resize(28);
84  for (auto x = 0; x < 900; ++x) {
85  s0 << t0;
86  }
87  t0.resize(100);
88  for (auto x = 0; x < 101; ++x) {
89  s0 << t0;
90  }
91  while (state.KeepRunning()) {
93  for (auto x = 0; x < 1000; ++x) {
94  s0 >> t1;
95  }
96  s0.Init(SER_NETWORK, 0);
97  }
98 }
99 
100 #define PREVECTOR_TEST(name, nontrivops, trivops) \
101  static void Prevector ## name ## Nontrivial(benchmark::State& state) { \
102  Prevector ## name<nontrivial_t>(state); \
103  } \
104  BENCHMARK(Prevector ## name ## Nontrivial, nontrivops); \
105  static void Prevector ## name ## Trivial(benchmark::State& state) { \
106  Prevector ## name<trivial_t>(state); \
107  } \
108  BENCHMARK(Prevector ## name ## Trivial, trivops);
109 
110 PREVECTOR_TEST(Clear, 28300, 88600)
111 PREVECTOR_TEST(Destructor, 28800, 88900)
112 PREVECTOR_TEST(Resize, 28900, 90300)
113 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:35
void clear()
Definition: prevector.h:343
bool KeepRunning()
Definition: bench.h:72
static void PrevectorResize(benchmark::State &state)
Definition: prevector.cpp:64
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
#define PREVECTOR_TEST(name, nontrivops, trivops)
Definition: prevector.cpp:100
static void PrevectorClear(benchmark::State &state)
Definition: prevector.cpp:48
unsigned char trivial_t
Definition: prevector.cpp:28
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:195
static void PrevectorDeserialize(benchmark::State &state)
Definition: prevector.cpp:79
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:186
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Definition: prevector.cpp:25