Bitcoin Core 29.99.0
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2022 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 <type_traits>
13#include <vector>
14
16{
17 int x{-1};
18 nontrivial_t() = default;
20};
21
22static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
23 "expected nontrivial_t to not be trivially constructible");
24
25typedef unsigned char trivial_t;
26static_assert(std::is_trivially_default_constructible_v<trivial_t>,
27 "expected trivial_t to be trivially constructible");
28
29template <typename T>
31{
32 bench.batch(2).run([&] {
37 });
38}
39
40template <typename T>
42{
45 bench.batch(2).run([&] {
47 t0.clear();
49 t1.clear();
50 });
51}
52
53template <typename T>
55{
58 bench.batch(4).run([&] {
60 t0.resize(0);
62 t1.resize(0);
63 });
64}
65
66template <typename T>
68{
69 DataStream s0{};
72 for (auto x = 0; x < 900; ++x) {
73 s0 << t0;
74 }
75 t0.resize(100);
76 for (auto x = 0; x < 101; ++x) {
77 s0 << t0;
78 }
79 bench.batch(1000).run([&] {
81 for (auto x = 0; x < 1000; ++x) {
82 s0 >> t1;
83 }
84 s0.Rewind();
85 });
86}
87
88template <typename T>
90{
91 bench.run([&] {
92 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
93 vec.reserve(260);
94 for (size_t i = 0; i < 260; ++i) {
95 vec.emplace_back();
96 }
97 });
98}
99
100
101template <typename T>
103{
104 bench.run([&] {
105 std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
106 vec.reserve(260);
107 for (size_t i = 0; i < 260; ++i) {
108 // force allocation
109 vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
110 }
111 });
112}
113
114#define PREVECTOR_TEST(name) \
115 static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
116 { \
117 Prevector##name<nontrivial_t>(bench); \
118 } \
119 BENCHMARK(Prevector##name##Nontrivial, benchmark::PriorityLevel::HIGH); \
120 static void Prevector##name##Trivial(benchmark::Bench& bench) \
121 { \
122 Prevector##name<trivial_t>(bench); \
123 } \
124 BENCHMARK(Prevector##name##Trivial, benchmark::PriorityLevel::HIGH);
125
126PREVECTOR_TEST(Clear)
127PREVECTOR_TEST(Destructor)
128PREVECTOR_TEST(Resize)
129PREVECTOR_TEST(Deserialize)
130PREVECTOR_TEST(FillVectorDirect)
131PREVECTOR_TEST(FillVectorIndirect)
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:102
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:54
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:89
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:30
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:41
unsigned char trivial_t
Definition: prevector.cpp:25
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:114
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:67
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:130
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1258
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:311
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:41
void resize(size_type new_size)
Definition: prevector.h:284
#define T(expected, seed, data)
#define READWRITE(...)
Definition: serialize.h:145
nontrivial_t()=default
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:19