diff --git a/src/bench/merkle_root.cpp b/src/bench/merkle_root.cpp index 5be592707ff..0e4c779f142 100644 --- a/src/bench/merkle_root.cpp +++ b/src/bench/merkle_root.cpp @@ -24,9 +24,9 @@ static void MerkleRoot(benchmark::Bench& bench) for (bool mutate : {false, true}) { bench.name(mutate ? "MerkleRootWithMutation" : "MerkleRoot").batch(hashes.size()).unit("leaf").run([&] { std::vector leaves; - leaves.resize(hashes.size()); + leaves.reserve((hashes.size() + 1) & ~1ULL); // capacity rounded up to even for (size_t s = 0; s < hashes.size(); s++) { - leaves[s] = hashes[s]; + leaves.push_back(hashes[s]); } bool mutated{false}; diff --git a/src/consensus/merkle.cpp b/src/consensus/merkle.cpp index 703a824e16f..b0819a1763e 100644 --- a/src/consensus/merkle.cpp +++ b/src/consensus/merkle.cpp @@ -66,9 +66,9 @@ uint256 ComputeMerkleRoot(std::vector hashes, bool* mutated) { uint256 BlockMerkleRoot(const CBlock& block, bool* mutated) { std::vector leaves; - leaves.resize(block.vtx.size()); + leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even for (size_t s = 0; s < block.vtx.size(); s++) { - leaves[s] = block.vtx[s]->GetHash().ToUint256(); + leaves.push_back(block.vtx[s]->GetHash().ToUint256()); } return ComputeMerkleRoot(std::move(leaves), mutated); } @@ -76,10 +76,10 @@ uint256 BlockMerkleRoot(const CBlock& block, bool* mutated) uint256 BlockWitnessMerkleRoot(const CBlock& block) { std::vector leaves; - leaves.resize(block.vtx.size()); - leaves[0].SetNull(); // The witness hash of the coinbase is 0. + leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even + leaves.emplace_back(); // The witness hash of the coinbase is 0. for (size_t s = 1; s < block.vtx.size(); s++) { - leaves[s] = block.vtx[s]->GetWitnessHash().ToUint256(); + leaves.push_back(block.vtx[s]->GetWitnessHash().ToUint256()); } return ComputeMerkleRoot(std::move(leaves)); } diff --git a/src/signet.cpp b/src/signet.cpp index 6524ebff45e..6c1df371aa9 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -58,10 +58,10 @@ static bool FetchAndClearCommitmentSection(const std::span header static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block) { std::vector leaves; - leaves.resize(block.vtx.size()); - leaves[0] = cb.GetHash().ToUint256(); + leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even + leaves.push_back(cb.GetHash().ToUint256()); for (size_t s = 1; s < block.vtx.size(); ++s) { - leaves[s] = block.vtx[s]->GetHash().ToUint256(); + leaves.push_back(block.vtx[s]->GetHash().ToUint256()); } return ComputeMerkleRoot(std::move(leaves)); }