Bitcoin ABC 0.32.4
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
7#include <script/script.h>
8#include <serialize.h>
9#include <streams.h>
10#include <type_traits>
11
12#include <bench/bench.h>
13
14// GCC 4.8 is missing some C++11 type_traits,
15// https://www.gnu.org/software/gcc/gcc-5/changes.html
16#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
17#define IS_TRIVIALLY_CONSTRUCTIBLE std::has_trivial_default_constructor
18#else
19#define IS_TRIVIALLY_CONSTRUCTIBLE std::is_trivially_default_constructible
20#endif
21
23 int x{-1};
24 nontrivial_t() = default;
26};
27
28static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
29 "expected nontrivial_t to not be trivially constructible");
30
31typedef uint8_t trivial_t;
32static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
33 "expected trivial_t to be trivially constructible");
34
35template <typename T> static void PrevectorDestructor(benchmark::Bench &bench) {
36 bench.batch(2).run([&] {
41 });
42}
43
44template <typename T> static void PrevectorClear(benchmark::Bench &bench) {
47 bench.batch(2).run([&] {
49 t0.clear();
51 t1.clear();
52 });
53}
54
55template <typename T> static void PrevectorResize(benchmark::Bench &bench) {
58 bench.batch(4).run([&] {
60 t0.resize(0);
62 t1.resize(0);
63 });
64}
65
66template <typename T>
68 DataStream s0{};
71 for (auto x = 0; x < 900; ++x) {
72 s0 << t0;
73 }
74 t0.resize(100);
75 for (auto x = 0; x < 101; ++x) {
76 s0 << t0;
77 }
78 bench.batch(1000).run([&] {
80 for (auto x = 0; x < 1000; ++x) {
81 s0 >> t1;
82 }
83 s0.Rewind();
84 });
85}
86
87template <typename T>
89 bench.run([&] {
90 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
91 vec.reserve(260);
92 for (size_t i = 0; i < 260; ++i) {
93 vec.emplace_back();
94 }
95 });
96}
97
98template <typename T>
100 bench.run([&] {
101 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
102 vec.reserve(260);
103 for (size_t i = 0; i < 260; ++i) {
104 // force allocation
105 vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
106 }
107 });
108}
109
110#define PREVECTOR_TEST(name) \
111 static void Prevector##name##Nontrivial(benchmark::Bench &bench) { \
112 Prevector##name<nontrivial_t>(bench); \
113 } \
114 BENCHMARK(Prevector##name##Nontrivial); \
115 static void Prevector##name##Trivial(benchmark::Bench &bench) { \
116 Prevector##name<trivial_t>(bench); \
117 } \
118 BENCHMARK(Prevector##name##Trivial);
119
120PREVECTOR_TEST(Clear)
121PREVECTOR_TEST(Destructor)
122PREVECTOR_TEST(Resize)
123PREVECTOR_TEST(Deserialize)
124PREVECTOR_TEST(FillVectorDirect)
125PREVECTOR_TEST(FillVectorIndirect)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:173
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:451
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:40
void resize(size_type new_size)
Definition: prevector.h:426
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:99
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:55
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:88
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:35
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:44
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:110
uint8_t trivial_t
Definition: prevector.cpp:31
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:67
#define READWRITE(...)
Definition: serialize.h:168
nontrivial_t()=default
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:25