BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 <compat.h>
6 #include <prevector.h>
7 #include <serialize.h>
8 #include <streams.h>
9 
10 #include <bench/bench.h>
11 
12 struct nontrivial_t {
13  int x;
14  nontrivial_t() :x(-1) {}
16  template <typename Stream, typename Operation>
17  inline void SerializationOp(Stream& s, Operation ser_action) {READWRITE(x);}
18 };
19 static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
20  "expected nontrivial_t to not be trivially constructible");
21 
22 typedef unsigned char trivial_t;
23 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
24  "expected trivial_t to be trivially constructible");
25 
26 template <typename T>
27 static void PrevectorDestructor(benchmark::State& state)
28 {
29  while (state.KeepRunning()) {
30  for (auto x = 0; x < 1000; ++x) {
33  t0.resize(28);
34  t1.resize(29);
35  }
36  }
37 }
38 
39 template <typename T>
40 static void PrevectorClear(benchmark::State& state)
41 {
42 
43  while (state.KeepRunning()) {
44  for (auto x = 0; x < 1000; ++x) {
47  t0.resize(28);
48  t0.clear();
49  t1.resize(29);
50  t1.clear();
51  }
52  }
53 }
54 
55 template <typename T>
56 static void PrevectorResize(benchmark::State& state)
57 {
58  while (state.KeepRunning()) {
61  for (auto x = 0; x < 1000; ++x) {
62  t0.resize(28);
63  t0.resize(0);
64  t1.resize(29);
65  t1.resize(0);
66  }
67  }
68 }
69 
70 template <typename T>
71 static void PrevectorDeserialize(benchmark::State& state)
72 {
73  CDataStream s0(SER_NETWORK, 0);
75  t0.resize(28);
76  for (auto x = 0; x < 900; ++x) {
77  s0 << t0;
78  }
79  t0.resize(100);
80  for (auto x = 0; x < 101; ++x) {
81  s0 << t0;
82  }
83  while (state.KeepRunning()) {
85  for (auto x = 0; x < 1000; ++x) {
86  s0 >> t1;
87  }
88  s0.Init(SER_NETWORK, 0);
89  }
90 }
91 
92 #define PREVECTOR_TEST(name, nontrivops, trivops) \
93  static void Prevector ## name ## Nontrivial(benchmark::State& state) { \
94  Prevector ## name<nontrivial_t>(state); \
95  } \
96  BENCHMARK(Prevector ## name ## Nontrivial, nontrivops); \
97  static void Prevector ## name ## Trivial(benchmark::State& state) { \
98  Prevector ## name<trivial_t>(state); \
99  } \
100  BENCHMARK(Prevector ## name ## Trivial, trivops);
101 
102 PREVECTOR_TEST(Clear, 28300, 88600)
103 PREVECTOR_TEST(Destructor, 28800, 88900)
104 PREVECTOR_TEST(Resize, 28900, 90300)
105 PREVECTOR_TEST(Deserialize, 6800, 52000)
void resize(size_type new_size)
Definition: prevector.h:327
void clear()
Definition: prevector.h:354
bool KeepRunning()
Definition: bench.h:70
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:221
#define PREVECTOR_TEST(name, nontrivops, trivops)
Definition: prevector.cpp:92
unsigned char trivial_t
Definition: prevector.cpp:20
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:182
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:39
#define READWRITE(...)
Definition: serialize.h:173
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Definition: prevector.cpp:17