refactor: extract STATIC_SIZE constant to prevector

Co-authored-by: Anthony Towns <aj@erisian.com.au>
This commit is contained in:
Lőrinc
2025-04-22 13:00:34 +02:00
parent 3b188b8b3d
commit 756da2a994
3 changed files with 27 additions and 22 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,
@@ -30,9 +30,9 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
ECC_Context ecc_context{};
struct PrevectorJob {
prevector<PREVECTOR_SIZE, uint8_t> p;
prevector<CScriptBase::STATIC_SIZE, uint8_t> p;
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()()
{

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<CScriptBase::STATIC_SIZE, T> t0;
prevector<CScriptBase::STATIC_SIZE, T> t1;
t0.resize(CScriptBase::STATIC_SIZE);
t1.resize(CScriptBase::STATIC_SIZE + 1);
});
}
template <typename T>
static void PrevectorClear(benchmark::Bench& bench)
{
prevector<28, T> t0;
prevector<28, T> t1;
prevector<CScriptBase::STATIC_SIZE, T> t0;
prevector<CScriptBase::STATIC_SIZE, T> t1;
bench.batch(2).run([&] {
t0.resize(28);
t0.resize(CScriptBase::STATIC_SIZE);
t0.clear();
t1.resize(29);
t1.resize(CScriptBase::STATIC_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<CScriptBase::STATIC_SIZE, T> t0;
prevector<CScriptBase::STATIC_SIZE, T> t1;
bench.batch(4).run([&] {
t0.resize(28);
t0.resize(CScriptBase::STATIC_SIZE);
t0.resize(0);
t1.resize(29);
t1.resize(CScriptBase::STATIC_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<CScriptBase::STATIC_SIZE, T> t0;
t0.resize(CScriptBase::STATIC_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<CScriptBase::STATIC_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<CScriptBase::STATIC_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<CScriptBase::STATIC_SIZE, T>> vec;
vec.reserve(260);
for (size_t i = 0; i < 260; ++i) {
// force allocation
vec.emplace_back(29, T{});
vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});
}
});
}

View File

@@ -38,6 +38,8 @@ class prevector {
static_assert(std::is_trivially_copyable_v<T>);
public:
static constexpr unsigned int STATIC_SIZE{N};
typedef Size size_type;
typedef Diff difference_type;
typedef T value_type;