Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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#include <serialize.h>
7#include <streams.h>
8#include <type_traits>
9
10#include <bench/bench.h>
11
13 int x{-1};
14 nontrivial_t() = default;
16};
17static_assert(!std::is_trivially_default_constructible<nontrivial_t>::value,
18 "expected nontrivial_t to not be trivially constructible");
19
20typedef unsigned char trivial_t;
21static_assert(std::is_trivially_default_constructible<trivial_t>::value,
22 "expected trivial_t to be trivially constructible");
23
24template <typename T>
26{
27 bench.batch(2).run([&] {
30 t0.resize(28);
31 t1.resize(29);
32 });
33}
34
35template <typename T>
37{
40 bench.batch(2).run([&] {
41 t0.resize(28);
42 t0.clear();
43 t1.resize(29);
44 t1.clear();
45 });
46}
47
48template <typename T>
50{
53 bench.batch(4).run([&] {
54 t0.resize(28);
55 t0.resize(0);
56 t1.resize(29);
57 t1.resize(0);
58 });
59}
60
61template <typename T>
63{
64 DataStream s0{};
66 t0.resize(28);
67 for (auto x = 0; x < 900; ++x) {
68 s0 << t0;
69 }
70 t0.resize(100);
71 for (auto x = 0; x < 101; ++x) {
72 s0 << t0;
73 }
74 bench.batch(1000).run([&] {
76 for (auto x = 0; x < 1000; ++x) {
77 s0 >> t1;
78 }
79 s0.Rewind();
80 });
81}
82
83template <typename T>
85{
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
94
95template <typename T>
97{
98 bench.run([&] {
99 std::vector<prevector<28, T>> vec;
100 for (size_t i = 0; i < 260; ++i) {
101 // force allocation
102 vec.emplace_back(29, T{});
103 }
104 });
105}
106
107#define PREVECTOR_TEST(name) \
108 static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
109 { \
110 Prevector##name<nontrivial_t>(bench); \
111 } \
112 BENCHMARK(Prevector##name##Nontrivial, benchmark::PriorityLevel::HIGH); \
113 static void Prevector##name##Trivial(benchmark::Bench& bench) \
114 { \
115 Prevector##name<trivial_t>(bench); \
116 } \
117 BENCHMARK(Prevector##name##Trivial, benchmark::PriorityLevel::HIGH);
118
119PREVECTOR_TEST(Clear)
120PREVECTOR_TEST(Destructor)
121PREVECTOR_TEST(Resize)
122PREVECTOR_TEST(Deserialize)
123PREVECTOR_TEST(FillVectorDirect)
124PREVECTOR_TEST(FillVectorIndirect)
static void PrevectorFillVectorIndirect(benchmark::Bench &bench)
Definition prevector.cpp:96
static void PrevectorResize(benchmark::Bench &bench)
Definition prevector.cpp:49
static void PrevectorFillVectorDirect(benchmark::Bench &bench)
Definition prevector.cpp:84
static void PrevectorDestructor(benchmark::Bench &bench)
Definition prevector.cpp:25
static void PrevectorClear(benchmark::Bench &bench)
Definition prevector.cpp:36
unsigned char trivial_t
Definition prevector.cpp:20
#define PREVECTOR_TEST(name)
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition prevector.cpp:62
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:357
void resize(size_type new_size)
Definition prevector.h:330
#define T(expected, seed, data)
#define READWRITE(...)
Definition serialize.h:156
nontrivial_t()=default
SERIALIZE_METHODS(nontrivial_t, obj)
Definition prevector.cpp:15