Skip to content

Commit 756da2a

Browse files
l0rincajtowns
andcommitted
refactor: extract STATIC_SIZE constant to prevector
Co-authored-by: Anthony Towns <[email protected]>
1 parent 3b188b8 commit 756da2a

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/bench/checkqueue.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <key.h>
99
#include <prevector.h>
1010
#include <random.h>
11+
#include <script/script.h>
1112

1213
#include <cstddef>
1314
#include <cstdint>
@@ -16,7 +17,6 @@
1617

1718
static const size_t BATCHES = 101;
1819
static const size_t BATCH_SIZE = 30;
19-
static const int PREVECTOR_SIZE = 28;
2020
static const unsigned int QUEUE_BATCH_SIZE = 128;
2121

2222
// This Benchmark tests the CheckQueue with a slightly realistic workload,
@@ -30,9 +30,9 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
3030
ECC_Context ecc_context{};
3131

3232
struct PrevectorJob {
33-
prevector<PREVECTOR_SIZE, uint8_t> p;
33+
prevector<CScriptBase::STATIC_SIZE, uint8_t> p;
3434
explicit PrevectorJob(FastRandomContext& insecure_rand){
35-
p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
35+
p.resize(insecure_rand.randrange(CScriptBase::STATIC_SIZE * 2));
3636
}
3737
std::optional<int> operator()()
3838
{

src/bench/prevector.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
#include <prevector.h>
66

77
#include <bench/bench.h>
8+
#include <script/script.h>
89
#include <serialize.h>
910
#include <streams.h>
1011

1112
#include <type_traits>
1213
#include <vector>
1314

14-
struct nontrivial_t {
15+
struct nontrivial_t
16+
{
1517
int x{-1};
1618
nontrivial_t() = default;
1719
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
1820
};
21+
1922
static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
2023
"expected nontrivial_t to not be trivially constructible");
2124

@@ -27,35 +30,35 @@ template <typename T>
2730
static void PrevectorDestructor(benchmark::Bench& bench)
2831
{
2932
bench.batch(2).run([&] {
30-
prevector<28, T> t0;
31-
prevector<28, T> t1;
32-
t0.resize(28);
33-
t1.resize(29);
33+
prevector<CScriptBase::STATIC_SIZE, T> t0;
34+
prevector<CScriptBase::STATIC_SIZE, T> t1;
35+
t0.resize(CScriptBase::STATIC_SIZE);
36+
t1.resize(CScriptBase::STATIC_SIZE + 1);
3437
});
3538
}
3639

3740
template <typename T>
3841
static void PrevectorClear(benchmark::Bench& bench)
3942
{
40-
prevector<28, T> t0;
41-
prevector<28, T> t1;
43+
prevector<CScriptBase::STATIC_SIZE, T> t0;
44+
prevector<CScriptBase::STATIC_SIZE, T> t1;
4245
bench.batch(2).run([&] {
43-
t0.resize(28);
46+
t0.resize(CScriptBase::STATIC_SIZE);
4447
t0.clear();
45-
t1.resize(29);
48+
t1.resize(CScriptBase::STATIC_SIZE + 1);
4649
t1.clear();
4750
});
4851
}
4952

5053
template <typename T>
5154
static void PrevectorResize(benchmark::Bench& bench)
5255
{
53-
prevector<28, T> t0;
54-
prevector<28, T> t1;
56+
prevector<CScriptBase::STATIC_SIZE, T> t0;
57+
prevector<CScriptBase::STATIC_SIZE, T> t1;
5558
bench.batch(4).run([&] {
56-
t0.resize(28);
59+
t0.resize(CScriptBase::STATIC_SIZE);
5760
t0.resize(0);
58-
t1.resize(29);
61+
t1.resize(CScriptBase::STATIC_SIZE + 1);
5962
t1.resize(0);
6063
});
6164
}
@@ -64,8 +67,8 @@ template <typename T>
6467
static void PrevectorDeserialize(benchmark::Bench& bench)
6568
{
6669
DataStream s0{};
67-
prevector<28, T> t0;
68-
t0.resize(28);
70+
prevector<CScriptBase::STATIC_SIZE, T> t0;
71+
t0.resize(CScriptBase::STATIC_SIZE);
6972
for (auto x = 0; x < 900; ++x) {
7073
s0 << t0;
7174
}
@@ -74,7 +77,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
7477
s0 << t0;
7578
}
7679
bench.batch(1000).run([&] {
77-
prevector<28, T> t1;
80+
prevector<CScriptBase::STATIC_SIZE, T> t1;
7881
for (auto x = 0; x < 1000; ++x) {
7982
s0 >> t1;
8083
}
@@ -86,7 +89,7 @@ template <typename T>
8689
static void PrevectorFillVectorDirect(benchmark::Bench& bench)
8790
{
8891
bench.run([&] {
89-
std::vector<prevector<28, T>> vec;
92+
std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
9093
vec.reserve(260);
9194
for (size_t i = 0; i < 260; ++i) {
9295
vec.emplace_back();
@@ -99,11 +102,11 @@ template <typename T>
99102
static void PrevectorFillVectorIndirect(benchmark::Bench& bench)
100103
{
101104
bench.run([&] {
102-
std::vector<prevector<28, T>> vec;
105+
std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
103106
vec.reserve(260);
104107
for (size_t i = 0; i < 260; ++i) {
105108
// force allocation
106-
vec.emplace_back(29, T{});
109+
vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
107110
}
108111
});
109112
}

src/prevector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class prevector {
3838
static_assert(std::is_trivially_copyable_v<T>);
3939

4040
public:
41+
static constexpr unsigned int STATIC_SIZE{N};
42+
4143
typedef Size size_type;
4244
typedef Diff difference_type;
4345
typedef T value_type;

0 commit comments

Comments
 (0)