Bitcoin Core  22.99.0
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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 struct nontrivial_t {
13  int x;
14  nontrivial_t() :x(-1) {}
16 };
17 static_assert(!std::is_trivially_default_constructible<nontrivial_t>::value,
18  "expected nontrivial_t to not be trivially constructible");
19 
20 typedef unsigned char trivial_t;
21 static_assert(std::is_trivially_default_constructible<trivial_t>::value,
22  "expected trivial_t to be trivially constructible");
23 
24 template <typename T>
26 {
27  bench.batch(2).run([&] {
30  t0.resize(28);
31  t1.resize(29);
32  });
33 }
34 
35 template <typename T>
36 static void PrevectorClear(benchmark::Bench& bench)
37 {
40  bench.batch(2).run([&] {
41  t0.resize(28);
42  t0.clear();
43  t1.resize(29);
44  t1.clear();
45  });
46 }
47 
48 template <typename T>
49 static void PrevectorResize(benchmark::Bench& bench)
50 {
53  bench.batch(4).run([&] {
54  t0.resize(28);
55  t0.resize(0);
56  t1.resize(29);
57  t1.resize(0);
58  });
59 }
60 
61 template <typename T>
63 {
64  CDataStream s0(SER_NETWORK, 0);
66  t0.resize(28);
67  for (auto x = 0; x < 900; ++x) {
68  s0 << t0;
69  }
70  t0.resize(100);
71  for (auto x = 0; x < 101; ++x) {
72  s0 << t0;
73  }
74  bench.batch(1000).run([&] {
76  for (auto x = 0; x < 1000; ++x) {
77  s0 >> t1;
78  }
79  s0.Rewind();
80  });
81 }
82 
83 #define PREVECTOR_TEST(name) \
84  static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
85  { \
86  Prevector##name<nontrivial_t>(bench); \
87  } \
88  BENCHMARK(Prevector##name##Nontrivial); \
89  static void Prevector##name##Trivial(benchmark::Bench& bench) \
90  { \
91  Prevector##name<trivial_t>(bench); \
92  } \
93  BENCHMARK(Prevector##name##Trivial);
94 
95 PREVECTOR_TEST(Clear)
96 PREVECTOR_TEST(Destructor)
97 PREVECTOR_TEST(Resize)
98 PREVECTOR_TEST(Deserialize)
trivial_t
unsigned char trivial_t
Definition: prevector.cpp:18
ankerl::nanobench::Bench::batch
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
streams.h
prevector::clear
void clear()
Definition: prevector.h:343
nontrivial_t::x
int x
Definition: prevector.cpp:13
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:614
PrevectorResize
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:49
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1181
CDataStream::Rewind
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:338
nontrivial_t::SERIALIZE_METHODS
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:15
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
nontrivial_t::nontrivial_t
nontrivial_t()
Definition: prevector.cpp:14
nontrivial_t
Definition: prevector.cpp:12
bench.h
prevector::resize
void resize(size_type new_size)
Definition: prevector.h:316
prevector
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
prevector.h
PrevectorDestructor
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:25
PrevectorClear
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:36
PREVECTOR_TEST
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:83
PrevectorDeserialize
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:62