refactor: extract PREVECTOR_SIZE to header constant

Co-authored-by: Luke Dashjr <luke-jr+git@utopios.org>
Co-authored-by: kuegi <29012906+kuegi@users.noreply.github.com>
This commit is contained in:
Lőrinc 2025-04-22 13:00:34 +02:00
parent c5e44a0435
commit a5f2ffa951
3 changed files with 25 additions and 21 deletions

View file

@ -8,6 +8,7 @@
#include <key.h>
#include <prevector.h>
#include <random.h>
#include <script/script.h>
#include <cstddef>
#include <cstdint>
@ -16,7 +17,6 @@
static const size_t BATCHES = 101;
static const size_t BATCH_SIZE = 30;
static const int PREVECTOR_SIZE = 28;
static const unsigned int QUEUE_BATCH_SIZE = 128;
// This Benchmark tests the CheckQueue with a slightly realistic workload,

View file

@ -5,17 +5,20 @@
#include <prevector.h>
#include <bench/bench.h>
#include <script/script.h>
#include <serialize.h>
#include <streams.h>
#include <type_traits>
#include <vector>
struct nontrivial_t {
struct nontrivial_t
{
int x{-1};
nontrivial_t() = default;
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
};
static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
"expected nontrivial_t to not be trivially constructible");
@ -27,22 +30,22 @@ template <typename T>
static void PrevectorDestructor(benchmark::Bench& bench)
{
bench.batch(2).run([&] {
prevector<28, T> t0;
prevector<28, T> t1;
t0.resize(28);
t1.resize(29);
prevector<PREVECTOR_SIZE, T> t0;
prevector<PREVECTOR_SIZE, T> t1;
t0.resize(PREVECTOR_SIZE);
t1.resize(PREVECTOR_SIZE + 1);
});
}
template <typename T>
static void PrevectorClear(benchmark::Bench& bench)
{
prevector<28, T> t0;
prevector<28, T> t1;
prevector<PREVECTOR_SIZE, T> t0;
prevector<PREVECTOR_SIZE, T> t1;
bench.batch(2).run([&] {
t0.resize(28);
t0.resize(PREVECTOR_SIZE);
t0.clear();
t1.resize(29);
t1.resize(PREVECTOR_SIZE + 1);
t1.clear();
});
}
@ -50,12 +53,12 @@ static void PrevectorClear(benchmark::Bench& bench)
template <typename T>
static void PrevectorResize(benchmark::Bench& bench)
{
prevector<28, T> t0;
prevector<28, T> t1;
prevector<PREVECTOR_SIZE, T> t0;
prevector<PREVECTOR_SIZE, T> t1;
bench.batch(4).run([&] {
t0.resize(28);
t0.resize(PREVECTOR_SIZE);
t0.resize(0);
t1.resize(29);
t1.resize(PREVECTOR_SIZE + 1);
t1.resize(0);
});
}
@ -64,8 +67,8 @@ template <typename T>
static void PrevectorDeserialize(benchmark::Bench& bench)
{
DataStream s0{};
prevector<28, T> t0;
t0.resize(28);
prevector<PREVECTOR_SIZE, T> t0;
t0.resize(PREVECTOR_SIZE);
for (auto x = 0; x < 900; ++x) {
s0 << t0;
}
@ -74,7 +77,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
s0 << t0;
}
bench.batch(1000).run([&] {
prevector<28, T> t1;
prevector<PREVECTOR_SIZE, T> t1;
for (auto x = 0; x < 1000; ++x) {
s0 >> t1;
}
@ -86,7 +89,7 @@ template <typename T>
static void PrevectorFillVectorDirect(benchmark::Bench& bench)
{
bench.run([&] {
std::vector<prevector<28, T>> vec;
std::vector<prevector<PREVECTOR_SIZE, T>> vec;
vec.reserve(260);
for (size_t i = 0; i < 260; ++i) {
vec.emplace_back();
@ -99,11 +102,11 @@ template <typename T>
static void PrevectorFillVectorIndirect(benchmark::Bench& bench)
{
bench.run([&] {
std::vector<prevector<28, T>> vec;
std::vector<prevector<PREVECTOR_SIZE, T>> vec;
vec.reserve(260);
for (size_t i = 0; i < 260; ++i) {
// force allocation
vec.emplace_back(29, T{});
vec.emplace_back(PREVECTOR_SIZE + 1, T{});
}
});
}

View file

@ -406,7 +406,8 @@ private:
* Tests in October 2015 showed use of this reduced dbcache memory usage by 23%
* and made an initial sync 13% faster.
*/
typedef prevector<28, unsigned char> CScriptBase;
static constexpr unsigned int PREVECTOR_SIZE{28};
typedef prevector<PREVECTOR_SIZE, unsigned char> CScriptBase;
bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);