Bitcoin Core 31.99.0
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-present 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 <bench/bench.h>
8#include <script/script.h>
9#include <serialize.h>
10#include <streams.h>
11
12#include <span>
13#include <type_traits>
14#include <vector>
15
17{
18 int x{-1};
19 nontrivial_t() = default;
21};
22
23static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
24 "expected nontrivial_t to not be trivially constructible");
25
26typedef unsigned char trivial_t;
27static_assert(std::is_trivially_default_constructible_v<trivial_t>,
28 "expected trivial_t to be trivially constructible");
29
30template <typename T>
32{
33 bench.batch(2).run([&] {
38 });
39}
40
41template <typename T>
43{
46 bench.batch(2).run([&] {
48 t0.clear();
50 t1.clear();
51 });
52}
53
54template <typename T>
56{
59 bench.batch(4).run([&] {
61 t0.resize(0);
63 t1.resize(0);
64 });
65}
66
67template <typename T>
69{
73 for (auto x = 0; x < 900; ++x) {
74 data << t0;
75 }
76 t0.resize(100);
77 for (auto x = 0; x < 100; ++x) {
78 data << t0;
79 }
80 bench.batch(1000).run([&] {
81 SpanReader s0{data};
83 for (auto x = 0; x < 1000; ++x) {
84 s0 >> t1;
85 }
86 });
87}
88
89template <typename T>
91{
92 bench.run([&] {
93 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
94 vec.reserve(260);
95 for (size_t i = 0; i < 260; ++i) {
96 vec.emplace_back();
97 }
98 });
99}
100
101
102template <typename T>
104{
105 bench.run([&] {
106 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
107 vec.reserve(260);
108 for (size_t i = 0; i < 260; ++i) {
109 // force allocation
110 vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
111 }
112 });
113}
114
115#define PREVECTOR_TEST(name) \
116 static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
117 { \
118 Prevector##name<nontrivial_t>(bench); \
119 } \
120 BENCHMARK(Prevector##name##Nontrivial); \
121 static void Prevector##name##Trivial(benchmark::Bench& bench) \
122 { \
123 Prevector##name<trivial_t>(bench); \
124 } \
125 BENCHMARK(Prevector##name##Trivial);
126
127PREVECTOR_TEST(Clear)
128PREVECTOR_TEST(Destructor)
129PREVECTOR_TEST(Resize)
130PREVECTOR_TEST(Deserialize)
131PREVECTOR_TEST(FillVectorDirect)
132PREVECTOR_TEST(FillVectorIndirect)
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:103
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:55
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:90
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:31
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:42
unsigned char trivial_t
Definition: prevector.cpp:26
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:115
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:68
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:83
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1308
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1332
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
void clear()
Definition: prevector.h:303
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:41
void resize(size_type new_size)
Definition: prevector.h:276
#define T(expected, seed, data)
#define READWRITE(...)
Definition: serialize.h:148
nontrivial_t()=default
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:20