Bitcoin ABC 0.30.5
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
21 int x;
22 nontrivial_t() : x(-1) {}
24};
25static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
26 "expected nontrivial_t to not be trivially constructible");
27
28typedef uint8_t trivial_t;
29static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
30 "expected trivial_t to be trivially constructible");
31
32template <typename T> static void PrevectorDestructor(benchmark::Bench &bench) {
33 bench.batch(2).run([&] {
36 t0.resize(28);
37 t1.resize(29);
38 });
39}
40
41template <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
52template <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
63template <typename T>
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
84template <typename T>
86 bench.run([&] {
87 std::vector<prevector<28, T>> vec;
88 for (size_t i = 0; i < 260; ++i) {
89 vec.emplace_back();
90 }
91 });
92}
93
94template <typename T>
96 bench.run([&] {
97 std::vector<prevector<28, T>> vec;
98 for (size_t i = 0; i < 260; ++i) {
99 // force allocation
100 vec.emplace_back(29, T{});
101 }
102 });
103}
104
105#define PREVECTOR_TEST(name) \
106 static void Prevector##name##Nontrivial(benchmark::Bench &bench) { \
107 Prevector##name<nontrivial_t>(bench); \
108 } \
109 BENCHMARK(Prevector##name##Nontrivial); \
110 static void Prevector##name##Trivial(benchmark::Bench &bench) { \
111 Prevector##name<trivial_t>(bench); \
112 } \
113 BENCHMARK(Prevector##name##Trivial);
114
115PREVECTOR_TEST(Clear)
116PREVECTOR_TEST(Destructor)
117PREVECTOR_TEST(Resize)
118PREVECTOR_TEST(Deserialize)
119PREVECTOR_TEST(FillVectorDirect)
120PREVECTOR_TEST(FillVectorIndirect)
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
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
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:449
void resize(size_type new_size)
Definition: prevector.h:424
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:95
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:52
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:85
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:105
uint8_t trivial_t
Definition: prevector.cpp:28
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