Bitcoin Core 28.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 <serialize.h>
9#include <streams.h>
10
11#include <type_traits>
12#include <vector>
13
15 int x{-1};
16 nontrivial_t() = default;
18};
19static_assert(!std::is_trivially_default_constructible<nontrivial_t>::value,
20 "expected nontrivial_t to not be trivially constructible");
21
22typedef unsigned char trivial_t;
23static_assert(std::is_trivially_default_constructible<trivial_t>::value,
24 "expected trivial_t to be trivially constructible");
25
26template <typename T>
28{
29 bench.batch(2).run([&] {
32 t0.resize(28);
33 t1.resize(29);
34 });
35}
36
37template <typename T>
39{
42 bench.batch(2).run([&] {
43 t0.resize(28);
44 t0.clear();
45 t1.resize(29);
46 t1.clear();
47 });
48}
49
50template <typename T>
52{
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>
65{
66 DataStream s0{};
68 t0.resize(28);
69 for (auto x = 0; x < 900; ++x) {
70 s0 << t0;
71 }
72 t0.resize(100);
73 for (auto x = 0; x < 101; ++x) {
74 s0 << t0;
75 }
76 bench.batch(1000).run([&] {
78 for (auto x = 0; x < 1000; ++x) {
79 s0 >> t1;
80 }
81 s0.Rewind();
82 });
83}
84
85template <typename T>
87{
88 bench.run([&] {
89 std::vector<prevector<28, T>> vec;
90 vec.reserve(260);
91 for (size_t i = 0; i < 260; ++i) {
92 vec.emplace_back();
93 }
94 });
95}
96
97
98template <typename T>
100{
101 bench.run([&] {
102 std::vector<prevector<28, T>> vec;
103 vec.reserve(260);
104 for (size_t i = 0; i < 260; ++i) {
105 // force allocation
106 vec.emplace_back(29, T{});
107 }
108 });
109}
110
111#define PREVECTOR_TEST(name) \
112 static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
113 { \
114 Prevector##name<nontrivial_t>(bench); \
115 } \
116 BENCHMARK(Prevector##name##Nontrivial, benchmark::PriorityLevel::HIGH); \
117 static void Prevector##name##Trivial(benchmark::Bench& bench) \
118 { \
119 Prevector##name<trivial_t>(bench); \
120 } \
121 BENCHMARK(Prevector##name##Trivial, benchmark::PriorityLevel::HIGH);
122
123PREVECTOR_TEST(Clear)
124PREVECTOR_TEST(Destructor)
125PREVECTOR_TEST(Resize)
126PREVECTOR_TEST(Deserialize)
127PREVECTOR_TEST(FillVectorDirect)
128PREVECTOR_TEST(FillVectorIndirect)
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition: prevector.cpp:99
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:51
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition: prevector.cpp:86
static void PrevectorDestructor(benchmark::Bench &bench)
Definition: prevector.cpp:27
static void PrevectorClear(benchmark::Bench &bench)
Definition: prevector.cpp:38
unsigned char trivial_t
Definition: prevector.cpp:22
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:111
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:64
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
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:355
void resize(size_type new_size)
Definition: prevector.h:328
#define T(expected, seed, data)
#define READWRITE(...)
Definition: serialize.h:156
nontrivial_t()=default
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:17