Skip to content

Commit 7129c9e

Browse files
committed
Merge bitcoin/bitcoin#32827: mempool: Avoid needless vtx iteration during IBD
249889b orphanage: avoid vtx iteration when no orphans (furszy) 41ad2be mempool: Avoid expensive loop in `removeForBlock` during IBD (Lőrinc) Pull request description: During Initial Block Download, the mempool is usually empty, but `CTxMemPool::removeForBlock` is still called for every connected block where we: * iterate over every transaction in the block even though none will be found in the empty `mapTx`, always leaving `txs_removed_for_block` empty... * which is pre-allocated regardless with `40 bytes * vtx.size()`, even though it will always remain empty. Similarly to bitcoin/bitcoin#32730 (comment), this change introduces a minor performance & memory optimization by only executing the loop if any of the affected mempool maps have any contents. The second commit is cherry-picked from there since it's related to this change as well. ACKs for top commit: optout21: ACK 249889b glozow: ACK 249889b ismaelsadeeq: reACK 249889b Tree-SHA512: 80d06ff1515164529cdc3ad21db3041bb5b2a1d4b72ba9e6884cdf40c5f1477fee7479944b8bca32a6f0bf27c4e5501fccd085f6041a2dbb101438629cfb9e4b
2 parents 11c6a86 + 249889b commit 7129c9e

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/node/txorphanage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ bool TxOrphanageImpl::HaveTxToReconsider(NodeId peer)
567567
}
568568
void TxOrphanageImpl::EraseForBlock(const CBlock& block)
569569
{
570+
if (m_orphans.empty()) return;
571+
570572
std::set<Wtxid> wtxids_to_erase;
571573
for (const CTransactionRef& ptx : block.vtx) {
572574
const CTransaction& block_tx = *ptx;

src/txmempool.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -661,26 +661,25 @@ void CTxMemPool::removeConflicts(const CTransaction &tx)
661661
}
662662
}
663663

664-
/**
665-
* Called when a block is connected. Removes from mempool.
666-
*/
667664
void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight)
668665
{
666+
// Remove confirmed txs and conflicts when a new block is connected, updating the fee logic
669667
AssertLockHeld(cs);
670668
Assume(!m_have_changeset);
671669
std::vector<RemovedMempoolTransactionInfo> txs_removed_for_block;
672-
txs_removed_for_block.reserve(vtx.size());
673-
for (const auto& tx : vtx)
674-
{
675-
txiter it = mapTx.find(tx->GetHash());
676-
if (it != mapTx.end()) {
677-
setEntries stage;
678-
stage.insert(it);
679-
txs_removed_for_block.emplace_back(*it);
680-
RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
670+
if (mapTx.size() || mapNextTx.size() || mapDeltas.size()) {
671+
txs_removed_for_block.reserve(vtx.size());
672+
for (const auto& tx : vtx) {
673+
txiter it = mapTx.find(tx->GetHash());
674+
if (it != mapTx.end()) {
675+
setEntries stage;
676+
stage.insert(it);
677+
txs_removed_for_block.emplace_back(*it);
678+
RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
679+
}
680+
removeConflicts(*tx);
681+
ClearPrioritisation(tx->GetHash());
681682
}
682-
removeConflicts(*tx);
683-
ClearPrioritisation(tx->GetHash());
684683
}
685684
if (m_opts.signals) {
686685
m_opts.signals->MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight);

0 commit comments

Comments
 (0)