refactor: extract STATIC_SIZE constant to prevector

Co-authored-by: Anthony Towns <aj@erisian.com.au>
This commit is contained in:
LőrincandAnthony Towns committed 2025-07-25 16:23:37 -07:00
1 parent 3b188b8b3d
commit 756da2a994
3 files changed
+27 -22

No files matched your search

+3 -3
View File
@@ -8,6 +8,7 @@
#include <key.h> #include <key.h>
#include <prevector.h> #include <prevector.h>
#include <random.h> #include <random.h>
#include <script/script.h>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
@@ -16,7 +17,6 @@
static const size_t BATCHES = 101; static const size_t BATCHES = 101;
static const size_t BATCH_SIZE = 30; static const size_t BATCH_SIZE = 30;
static const int PREVECTOR_SIZE = 28;
static const unsigned int QUEUE_BATCH_SIZE = 128; static const unsigned int QUEUE_BATCH_SIZE = 128;
// This Benchmark tests the CheckQueue with a slightly realistic workload, // This Benchmark tests the CheckQueue with a slightly realistic workload,
@@ -30,9 +30,9 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
ECC_Context ecc_context{}; ECC_Context ecc_context{};
struct PrevectorJob { struct PrevectorJob {
prevector<PREVECTOR_SIZE, uint8_t> p; prevector<CScriptBase::STATIC_SIZE, uint8_t> p;
explicit PrevectorJob(FastRandomContext& insecure_rand){ explicit PrevectorJob(FastRandomContext& insecure_rand){
p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2)); p.resize(insecure_rand.randrange(CScriptBase::STATIC_SIZE * 2));
} }
std::optional<int> operator()() std::optional<int> operator()()
{ {
+22 -19
View File
@@ -5,17 +5,20 @@
#include <prevector.h> #include <prevector.h>
#include <bench/bench.h> #include <bench/bench.h>
#include <script/script.h>
#include <serialize.h> #include <serialize.h>
#include <streams.h> #include <streams.h>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
struct nontrivial_t { struct nontrivial_t
{
int x{-1}; int x{-1};
nontrivial_t() = default; nontrivial_t() = default;
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); } SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
}; };
static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>, static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
"expected nontrivial_t to not be trivially constructible"); "expected nontrivial_t to not be trivially constructible");
@@ -27,22 +30,22 @@ template <typename T>
static void PrevectorDestructor(benchmark::Bench& bench) static void PrevectorDestructor(benchmark::Bench& bench)
{ {
bench.batch(2).run([&] { bench.batch(2).run([&] {
prevector<28, T> t0; prevector<CScriptBase::STATIC_SIZE, T> t0;
prevector<28, T> t1; prevector<CScriptBase::STATIC_SIZE, T> t1;
t0.resize(28); t0.resize(CScriptBase::STATIC_SIZE);
t1.resize(29); t1.resize(CScriptBase::STATIC_SIZE + 1);
}); });
} }
template <typename T> template <typename T>
static void PrevectorClear(benchmark::Bench& bench) static void PrevectorClear(benchmark::Bench& bench)
{ {
prevector<28, T> t0; prevector<CScriptBase::STATIC_SIZE, T> t0;
prevector<28, T> t1; prevector<CScriptBase::STATIC_SIZE, T> t1;
bench.batch(2).run([&] { bench.batch(2).run([&] {
t0.resize(28); t0.resize(CScriptBase::STATIC_SIZE);
t0.clear(); t0.clear();
t1.resize(29); t1.resize(CScriptBase::STATIC_SIZE + 1);
t1.clear(); t1.clear();
}); });
} }
@@ -50,12 +53,12 @@ static void PrevectorClear(benchmark::Bench& bench)
template <typename T> template <typename T>
static void PrevectorResize(benchmark::Bench& bench) static void PrevectorResize(benchmark::Bench& bench)
{ {
prevector<28, T> t0; prevector<CScriptBase::STATIC_SIZE, T> t0;
prevector<28, T> t1; prevector<CScriptBase::STATIC_SIZE, T> t1;
bench.batch(4).run([&] { bench.batch(4).run([&] {
t0.resize(28); t0.resize(CScriptBase::STATIC_SIZE);
t0.resize(0); t0.resize(0);
t1.resize(29); t1.resize(CScriptBase::STATIC_SIZE + 1);
t1.resize(0); t1.resize(0);
}); });
} }
@@ -64,8 +67,8 @@ template <typename T>
static void PrevectorDeserialize(benchmark::Bench& bench) static void PrevectorDeserialize(benchmark::Bench& bench)
{ {
DataStream s0{}; DataStream s0{};
prevector<28, T> t0; prevector<CScriptBase::STATIC_SIZE, T> t0;
t0.resize(28); t0.resize(CScriptBase::STATIC_SIZE);
for (auto x = 0; x < 900; ++x) { for (auto x = 0; x < 900; ++x) {
s0 << t0; s0 << t0;
} }
@@ -74,7 +77,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
s0 << t0; s0 << t0;
} }
bench.batch(1000).run([&] { bench.batch(1000).run([&] {
prevector<28, T> t1; prevector<CScriptBase::STATIC_SIZE, T> t1;
for (auto x = 0; x < 1000; ++x) { for (auto x = 0; x < 1000; ++x) {
s0 >> t1; s0 >> t1;
} }
@@ -86,7 +89,7 @@ template <typename T>
static void PrevectorFillVectorDirect(benchmark::Bench& bench) static void PrevectorFillVectorDirect(benchmark::Bench& bench)
{ {
bench.run([&] { bench.run([&] {
std::vector<prevector<28, T>> vec; std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
vec.reserve(260); vec.reserve(260);
for (size_t i = 0; i < 260; ++i) { for (size_t i = 0; i < 260; ++i) {
vec.emplace_back(); vec.emplace_back();
@@ -99,11 +102,11 @@ template <typename T>
static void PrevectorFillVectorIndirect(benchmark::Bench& bench) static void PrevectorFillVectorIndirect(benchmark::Bench& bench)
{ {
bench.run([&] { bench.run([&] {
std::vector<prevector<28, T>> vec; std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;
vec.reserve(260); vec.reserve(260);
for (size_t i = 0; i < 260; ++i) { for (size_t i = 0; i < 260; ++i) {
// force allocation // force allocation
vec.emplace_back(29, T{}); vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
} }
}); });
} }
+2
View File
@@ -38,6 +38,8 @@ class prevector {
static_assert(std::is_trivially_copyable_v<T>); static_assert(std::is_trivially_copyable_v<T>);
public: public:
static constexpr unsigned int STATIC_SIZE{N};
typedef Size size_type; typedef Size size_type;
typedef Diff difference_type; typedef Diff difference_type;
typedef T value_type; typedef T value_type;