Make CTxMemPool::remove more effecient by avoiding recursion

This commit is contained in:
Matt Corallo 2014-11-18 14:57:20 -08:00
parent b7b4318f3a
commit 7fd6219af7
1 changed files with 18 additions and 12 deletions

View File

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