Skip to content

Commit

Permalink
Make CTxMemPool::remove more effecient by avoiding recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Dec 8, 2014
1 parent b7b4318 commit 7fd6219
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,32 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
}


void CTxMemPool::remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive)
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
{
// Remove transaction from memory pool
{
LOCK(cs);
uint256 hash = tx.GetHash();
if (fRecursive) {
for (unsigned int i = 0; i < tx.vout.size(); i++) {
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
if (it == mapNextTx.end())
continue;
remove(*it->second.ptx, removed, true);
}
}
if (mapTx.count(hash))
std::deque<uint256> txToRemove;
txToRemove.push_back(origTx.GetHash());
while (!txToRemove.empty())
{
removed.push_front(tx);
uint256 hash = txToRemove.front();
txToRemove.pop_front();
if (!mapTx.count(hash))
continue;
const CTransaction& tx = mapTx[hash].GetTx();
if (fRecursive) {
for (unsigned int i = 0; i < tx.vout.size(); i++) {
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
if (it == mapNextTx.end())
continue;
txToRemove.push_back(it->second.ptx->GetHash());
}
}
BOOST_FOREACH(const CTxIn& txin, tx.vin)
mapNextTx.erase(txin.prevout);

removed.push_back(tx);
totalTxSize -= mapTx[hash].GetTxSize();
mapTx.erase(hash);
nTransactionsUpdated++;
Expand Down

0 comments on commit 7fd6219

Please sign in to comment.