Skip to content

Commit 7fd6219

Browse files
committed
Make CTxMemPool::remove more effecient by avoiding recursion
1 parent b7b4318 commit 7fd6219

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/txmempool.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,26 +427,32 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
427427
}
428428

429429

430-
void CTxMemPool::remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive)
430+
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
431431
{
432432
// Remove transaction from memory pool
433433
{
434434
LOCK(cs);
435-
uint256 hash = tx.GetHash();
436-
if (fRecursive) {
437-
for (unsigned int i = 0; i < tx.vout.size(); i++) {
438-
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
439-
if (it == mapNextTx.end())
440-
continue;
441-
remove(*it->second.ptx, removed, true);
442-
}
443-
}
444-
if (mapTx.count(hash))
435+
std::deque<uint256> txToRemove;
436+
txToRemove.push_back(origTx.GetHash());
437+
while (!txToRemove.empty())
445438
{
446-
removed.push_front(tx);
439+
uint256 hash = txToRemove.front();
440+
txToRemove.pop_front();
441+
if (!mapTx.count(hash))
442+
continue;
443+
const CTransaction& tx = mapTx[hash].GetTx();
444+
if (fRecursive) {
445+
for (unsigned int i = 0; i < tx.vout.size(); i++) {
446+
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
447+
if (it == mapNextTx.end())
448+
continue;
449+
txToRemove.push_back(it->second.ptx->GetHash());
450+
}
451+
}
447452
BOOST_FOREACH(const CTxIn& txin, tx.vin)
448453
mapNextTx.erase(txin.prevout);
449454

455+
removed.push_back(tx);
450456
totalTxSize -= mapTx[hash].GetTxSize();
451457
mapTx.erase(hash);
452458
nTransactionsUpdated++;

0 commit comments

Comments
 (0)