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  bench.batch(2).run([&] {
38  t0.resize(28);
39  t1.resize(29);
40  });
41 }
42 
43 template <typename T>
44 static void PrevectorClear(benchmark::Bench& bench)
45 {
48  bench.batch(2).run([&] {
49  t0.resize(28);
50  t0.clear();
51  t1.resize(29);
52  t1.clear();
53  });
54 }
55 
56 template <typename T>
57 static void PrevectorResize(benchmark::Bench& bench)
58 {
61  bench.batch(4).run([&] {
62  t0.resize(28);
63  t0.resize(0);
64  t1.resize(29);
65  t1.resize(0);
66  });
67 }
68 
69 template <typename T>
71 {
72  CDataStream s0(SER_NETWORK, 0);
74  t0.resize(28);
75  for (auto x = 0; x < 900; ++x) {
76  s0 << t0;
77  }
78  t0.resize(100);
79  for (auto x = 0; x < 101; ++x) {
80  s0 << t0;
81  }
82  bench.batch(1000).run([&] {
84  for (auto x = 0; x < 1000; ++x) {
85  s0 >> t1;
86  }
87  s0.Init(SER_NETWORK, 0);
88  });
89 }
90 
91 #define PREVECTOR_TEST(name) \
92  static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
93  { \
94  Prevector##name<nontrivial_t>(bench); \
95  } \
96  BENCHMARK(Prevector##name##Nontrivial); \
97  static void Prevector##name##Trivial(benchmark::Bench& bench) \
98  { \
99  Prevector##name<trivial_t>(bench); \
100  } \
101  BENCHMARK(Prevector##name##Trivial);
102 
103 PREVECTOR_TEST(Clear)
104 PREVECTOR_TEST(Destructor)
105 PREVECTOR_TEST(Resize)
106 PREVECTOR_TEST(Deserialize)
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:260
void resize(size_type new_size)
Definition: prevector.h:316
void clear()
Definition: prevector.h:343
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:57
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:44
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:23
unsigned char trivial_t
Definition: prevector.cpp:26
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:91
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:70
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
#define READWRITE(...)
Definition: serialize.h:175
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:33
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.