diff --git a/src/bench/prevector.cpp b/src/bench/prevector.cpp index 9b83c42693fd3..adc3d18de000c 100644 --- a/src/bench/prevector.cpp +++ b/src/bench/prevector.cpp @@ -87,6 +87,7 @@ static void PrevectorFillVectorDirect(benchmark::Bench& bench) { bench.run([&] { std::vector> vec; + vec.reserve(260); for (size_t i = 0; i < 260; ++i) { vec.emplace_back(); } @@ -99,6 +100,7 @@ static void PrevectorFillVectorIndirect(benchmark::Bench& bench) { bench.run([&] { std::vector> vec; + vec.reserve(260); for (size_t i = 0; i < 260; ++i) { // force allocation vec.emplace_back(29, T{}); diff --git a/src/rpc/output_script.cpp b/src/rpc/output_script.cpp index 49f3a812437d5..5de2847be997d 100644 --- a/src/rpc/output_script.cpp +++ b/src/rpc/output_script.cpp @@ -123,6 +123,7 @@ static RPCHelpMan createmultisig() // Get the public keys const UniValue& keys = request.params[1].get_array(); std::vector pubkeys; + pubkeys.reserve(keys.size()); for (unsigned int i = 0; i < keys.size(); ++i) { pubkeys.push_back(HexToPubKey(keys[i].get_str())); } diff --git a/src/test/checkqueue_tests.cpp b/src/test/checkqueue_tests.cpp index a9a1e4707067a..b7014db4a5b9e 100644 --- a/src/test/checkqueue_tests.cpp +++ b/src/test/checkqueue_tests.cpp @@ -360,6 +360,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks) auto queue = std::make_unique(QUEUE_BATCH_SIZE, SCRIPT_CHECK_THREADS); { std::vector tg; + tg.reserve(3); std::atomic nThreads {0}; std::atomic fails {0}; for (size_t i = 0; i < 3; ++i) { diff --git a/src/test/cuckoocache_tests.cpp b/src/test/cuckoocache_tests.cpp index bb4c8c70938f3..f08c4d44287b6 100644 --- a/src/test/cuckoocache_tests.cpp +++ b/src/test/cuckoocache_tests.cpp @@ -224,6 +224,7 @@ void test_cache_erase_parallel(size_t megabytes) /** Spin up 3 threads to run contains with erase. */ std::vector threads; + threads.reserve(3); /** Erase the first quarter */ for (uint32_t x = 0; x < 3; ++x) /** Each thread is emplaced with x copy-by-value diff --git a/src/test/fuzz/txorphan.cpp b/src/test/fuzz/txorphan.cpp index 5129c05a3977f..31af1afff52d3 100644 --- a/src/test/fuzz/txorphan.cpp +++ b/src/test/fuzz/txorphan.cpp @@ -38,6 +38,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) TxOrphanage orphanage; std::vector outpoints; // Duplicates are tolerated + outpoints.reserve(200'000); // initial outpoints used to construct transactions later for (uint8_t i = 0; i < 4; i++) { @@ -55,12 +56,14 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange(1, 256); // pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not // running any transaction validation logic before adding transactions to the orphanage + tx_mut.vin.reserve(num_in); for (uint32_t i = 0; i < num_in; i++) { auto& prevout = PickValue(fuzzed_data_provider, outpoints); // try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange(0, CTxIn::SEQUENCE_FINAL)); } // output amount will not affect txorphanage + tx_mut.vout.reserve(num_out); for (uint32_t i = 0; i < num_out; i++) { tx_mut.vout.emplace_back(CAmount{0}, CScript{}); } diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index 8e2b8639c2895..38be59fb64b30 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -79,6 +79,7 @@ template { const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange(0, max_vector_size); std::vector r; + r.reserve(n_elements); for (size_t i = 0; i < n_elements; ++i) { r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length)); } @@ -90,6 +91,7 @@ template { const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange(0, max_vector_size); std::vector r; + r.reserve(n_elements); for (size_t i = 0; i < n_elements; ++i) { r.push_back(fuzzed_data_provider.ConsumeIntegral()); } diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 5089f3e8e33fe..87303d7417498 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -428,6 +428,7 @@ BOOST_AUTO_TEST_CASE(rpc_getblockstats_calculate_percentiles_by_weight) { int64_t total_weight = 200; std::vector> feerates; + feerates.reserve(200); CAmount result[NUM_GETBLOCKSTATS_PERCENTILES] = { 0 }; for (int64_t i = 0; i < 100; i++) {