Bitcoin Core 22.99.0
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2020 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;
14 nontrivial_t() :x(-1) {}
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{
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
83#define PREVECTOR_TEST(name) \
84 static void Prevector##name##Nontrivial(benchmark::Bench& bench) \
85 { \
86 Prevector##name<nontrivial_t>(bench); \
87 } \
88 BENCHMARK(Prevector##name##Nontrivial); \
89 static void Prevector##name##Trivial(benchmark::Bench& bench) \
90 { \
91 Prevector##name<trivial_t>(bench); \
92 } \
93 BENCHMARK(Prevector##name##Trivial);
94
95PREVECTOR_TEST(Clear)
96PREVECTOR_TEST(Destructor)
97PREVECTOR_TEST(Resize)
98PREVECTOR_TEST(Deserialize)
static void PrevectorResize(benchmark::Bench &bench)
Definition: prevector.cpp:49
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:18
#define PREVECTOR_TEST(name)
Definition: prevector.cpp:83
static void PrevectorDeserialize(benchmark::Bench &bench)
Definition: prevector.cpp:62
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:338
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:37
void clear()
Definition: prevector.h:343
void resize(size_type new_size)
Definition: prevector.h:316
@ SER_NETWORK
Definition: serialize.h:138
#define READWRITE(...)
Definition: serialize.h:147
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:15