Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2015-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <prevector.h>7#include <bench/bench.h>8#include <script/script.h>9#include <serialize.h>10#include <streams.h>12#include <span>13#include <type_traits>14#include <vector>16struct nontrivial_t17{18int x{-1};19nontrivial_t() = default;20SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }21};23static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,24"expected nontrivial_t to not be trivially constructible");26typedef unsigned char trivial_t;27static_assert(std::is_trivially_default_constructible_v<trivial_t>,28"expected trivial_t to be trivially constructible");30template <typename T>31static void PrevectorDestructor(benchmark::Bench& bench)32{33bench.batch(2).run([&] {34prevector<CScriptBase::STATIC_SIZE, T> t0;35prevector<CScriptBase::STATIC_SIZE, T> t1;36t0.resize(CScriptBase::STATIC_SIZE);37t1.resize(CScriptBase::STATIC_SIZE + 1);38});39}41template <typename T>42static void PrevectorClear(benchmark::Bench& bench)43{44prevector<CScriptBase::STATIC_SIZE, T> t0;45prevector<CScriptBase::STATIC_SIZE, T> t1;46bench.batch(2).run([&] {47t0.resize(CScriptBase::STATIC_SIZE);48t0.clear();49t1.resize(CScriptBase::STATIC_SIZE + 1);50t1.clear();51});52}54template <typename T>55static void PrevectorResize(benchmark::Bench& bench)56{57prevector<CScriptBase::STATIC_SIZE, T> t0;58prevector<CScriptBase::STATIC_SIZE, T> t1;59bench.batch(4).run([&] {60t0.resize(CScriptBase::STATIC_SIZE);61t0.resize(0);62t1.resize(CScriptBase::STATIC_SIZE + 1);63t1.resize(0);64});65}67template <typename T>68static void PrevectorDeserialize(benchmark::Bench& bench)69{70DataStream data{};71prevector<CScriptBase::STATIC_SIZE, T> t0;72t0.resize(CScriptBase::STATIC_SIZE);73for (auto x = 0; x < 900; ++x) {74data << t0;75}76t0.resize(100);77for (auto x = 0; x < 100; ++x) {78data << t0;79}80bench.batch(1000).run([&] {81SpanReader s0{data};82prevector<CScriptBase::STATIC_SIZE, T> t1;83for (auto x = 0; x < 1000; ++x) {84s0 >> t1;85}86});87}89template <typename T>90static void PrevectorFillVectorDirect(benchmark::Bench& bench)91{92bench.run([&] {93std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;94vec.reserve(260);95for (size_t i = 0; i < 260; ++i) {96vec.emplace_back();97}98});99}102template <typename T>103static void PrevectorFillVectorIndirect(benchmark::Bench& bench)104{105bench.run([&] {106std::vector<prevector<CScriptBase::STATIC_SIZE, T>> vec;107vec.reserve(260);108for (size_t i = 0; i < 260; ++i) {109// force allocation110vec.emplace_back(CScriptBase::STATIC_SIZE + 1, T{});111}112});113}115#define PREVECTOR_TEST(name) \116static void Prevector##name##Nontrivial(benchmark::Bench& bench) \117{ \118Prevector##name<nontrivial_t>(bench); \119} \120BENCHMARK(Prevector##name##Nontrivial); \121static void Prevector##name##Trivial(benchmark::Bench& bench) \122{ \123Prevector##name<trivial_t>(bench); \124} \125BENCHMARK(Prevector##name##Trivial);127PREVECTOR_TEST(Clear)128PREVECTOR_TEST(Destructor)129PREVECTOR_TEST(Resize)130PREVECTOR_TEST(Deserialize)131PREVECTOR_TEST(FillVectorDirect)132PREVECTOR_TEST(FillVectorIndirect)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.