Bitcoin ABC  0.29.2
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 uint8_t trivial_t;
29 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
30  "expected trivial_t to be trivially constructible");
31 
32 template <typename T> static void PrevectorDestructor(benchmark::Bench &bench) {
33  bench.batch(2).run([&] {
36  t0.resize(28);
37  t1.resize(29);
38  });
39 }
40 
41 template <typename T> static void PrevectorClear(benchmark::Bench &bench) {
44  bench.batch(2).run([&] {
45  t0.resize(28);
46  t0.clear();
47  t1.resize(29);
48  t1.clear();
49  });
50 }
51 
52 template <typename T> static void PrevectorResize(benchmark::Bench &bench) {
55  bench.batch(4).run([&] {
56  t0.resize(28);
57  t0.resize(0);
58  t1.resize(29);
59  t1.resize(0);
60  });
61 }
62 
63 template <typename T>
65  CDataStream s0(SER_NETWORK, 0);
67  t0.resize(28);
68  for (auto x = 0; x < 900; ++x) {
69  s0 << t0;
70  }
71  t0.resize(100);
72  for (auto x = 0; x < 101; ++x) {
73  s0 << t0;
74  }
75  bench.batch(1000).run([&] {
77  for (auto x = 0; x < 1000; ++x) {
78  s0 >> t1;
79  }
80  s0.Rewind();
81  });
82 }
83 
84 #define PREVECTOR_TEST(name) \
85  static void Prevector##name##Nontrivial(benchmark::Bench &bench) { \
86  Prevector##name<nontrivial_t>(bench); \
87  } \
88  BENCHMARK(Prevector##name##Nontrivial); \
89  static void Prevector##name##Trivial(benchmark::Bench &bench) { \
90  Prevector##name<trivial_t>(bench); \
91  } \
92  BENCHMARK(Prevector##name##Trivial);
93 
94 PREVECTOR_TEST(Clear)
95 PREVECTOR_TEST(Destructor)
96 PREVECTOR_TEST(Resize)
97 PREVECTOR_TEST(Deserialize)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:177
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:315
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
void clear()
Definition: prevector.h:441
void resize(size_type new_size)
Definition: prevector.h:416
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:52
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:32
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:41
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:84
uint8_t trivial_t
Definition: prevector.cpp:26
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:64
@ SER_NETWORK
Definition: serialize.h:152
#define READWRITE(...)
Definition: serialize.h:166
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:23