Skip to content

Commit 8f2f1e0

Browse files
committed
wallet: Avoid second mapWallet lookup
1 parent aeb3175 commit 8f2f1e0

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

src/qt/walletmodel.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,11 @@ void WalletModel::getOutputs(const std::vector<COutPoint>& vOutpoints, std::vect
577577
LOCK2(cs_main, wallet->cs_wallet);
578578
for (const COutPoint& outpoint : vOutpoints)
579579
{
580-
if (!wallet->mapWallet.count(outpoint.hash)) continue;
581-
int nDepth = wallet->mapWallet[outpoint.hash].GetDepthInMainChain();
580+
auto it = wallet->mapWallet.find(outpoint.hash);
581+
if (it == wallet->mapWallet.end()) continue;
582+
int nDepth = it->second.GetDepthInMainChain();
582583
if (nDepth < 0) continue;
583-
COutput out(&wallet->mapWallet[outpoint.hash], outpoint.n, nDepth, true /* spendable */, true /* solvable */, true /* safe */);
584+
COutput out(&it->second, outpoint.n, nDepth, true /* spendable */, true /* solvable */, true /* safe */);
584585
vOutputs.push_back(out);
585586
}
586587
}

src/wallet/feebumper.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
7676
vErrors.clear();
7777
bumpedTxid.SetNull();
7878
AssertLockHeld(pWallet->cs_wallet);
79-
if (!pWallet->mapWallet.count(txid)) {
79+
auto it = pWallet->mapWallet.find(txid);
80+
if (it == pWallet->mapWallet.end()) {
8081
vErrors.push_back("Invalid or non-wallet transaction id");
8182
currentResult = BumpFeeResult::INVALID_ADDRESS_OR_KEY;
8283
return;
8384
}
84-
auto it = pWallet->mapWallet.find(txid);
8585
const CWalletTx& wtx = it->second;
8686

8787
if (!preconditionChecks(pWallet, wtx)) {
@@ -241,12 +241,13 @@ bool CFeeBumper::commit(CWallet *pWallet)
241241
if (!vErrors.empty() || currentResult != BumpFeeResult::OK) {
242242
return false;
243243
}
244-
if (txid.IsNull() || !pWallet->mapWallet.count(txid)) {
244+
auto it = txid.IsNull() ? pWallet->mapWallet.end() : pWallet->mapWallet.find(txid);
245+
if (it == pWallet->mapWallet.end()) {
245246
vErrors.push_back("Invalid or non-wallet transaction id");
246247
currentResult = BumpFeeResult::MISC_ERROR;
247248
return false;
248249
}
249-
CWalletTx& oldWtx = pWallet->mapWallet[txid];
250+
CWalletTx& oldWtx = it->second;
250251

251252
// make sure the transaction still has no descendants and hasn't been mined in the meantime
252253
if (!preconditionChecks(pWallet, oldWtx)) {

src/wallet/rpcwallet.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,10 +1874,11 @@ UniValue listsinceblock(const JSONRPCRequest& request)
18741874
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
18751875
}
18761876
for (const CTransactionRef& tx : block.vtx) {
1877-
if (pwallet->mapWallet.count(tx->GetHash()) > 0) {
1877+
auto it = pwallet->mapWallet.find(tx->GetHash());
1878+
if (it != pwallet->mapWallet.end()) {
18781879
// We want all transactions regardless of confirmation count to appear here,
18791880
// even negative confirmation ones, hence the big negative.
1880-
ListTransactions(pwallet, pwallet->mapWallet[tx->GetHash()], "*", -100000000, true, removed, filter);
1881+
ListTransactions(pwallet, it->second, "*", -100000000, true, removed, filter);
18811882
}
18821883
}
18831884
paltindex = paltindex->pprev;
@@ -1957,10 +1958,11 @@ UniValue gettransaction(const JSONRPCRequest& request)
19571958
filter = filter | ISMINE_WATCH_ONLY;
19581959

19591960
UniValue entry(UniValue::VOBJ);
1960-
if (!pwallet->mapWallet.count(hash)) {
1961+
auto it = pwallet->mapWallet.find(hash);
1962+
if (it == pwallet->mapWallet.end()) {
19611963
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id");
19621964
}
1963-
const CWalletTx& wtx = pwallet->mapWallet[hash];
1965+
const CWalletTx& wtx = it->second;
19641966

19651967
CAmount nCredit = wtx.GetCredit(filter);
19661968
CAmount nDebit = wtx.GetDebit(filter);

src/wallet/wallet.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,9 @@ void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)
590590

591591
void CWallet::AddToSpends(const uint256& wtxid)
592592
{
593-
assert(mapWallet.count(wtxid));
594-
CWalletTx& thisTx = mapWallet[wtxid];
593+
auto it = mapWallet.find(wtxid);
594+
assert(it != mapWallet.end());
595+
CWalletTx& thisTx = it->second;
595596
if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
596597
return;
597598

@@ -974,8 +975,9 @@ bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
974975
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
975976
AddToSpends(hash);
976977
for (const CTxIn& txin : wtx.tx->vin) {
977-
if (mapWallet.count(txin.prevout.hash)) {
978-
CWalletTx& prevtx = mapWallet[txin.prevout.hash];
978+
auto it = mapWallet.find(txin.prevout.hash);
979+
if (it != mapWallet.end()) {
980+
CWalletTx& prevtx = it->second;
979981
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
980982
MarkConflicted(prevtx.hashBlock, wtx.GetHash());
981983
}
@@ -1050,8 +1052,9 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10501052
std::set<uint256> done;
10511053

10521054
// Can't mark abandoned if confirmed or in mempool
1053-
assert(mapWallet.count(hashTx));
1054-
CWalletTx& origtx = mapWallet[hashTx];
1055+
auto it = mapWallet.find(hashTx);
1056+
assert(it != mapWallet.end());
1057+
CWalletTx& origtx = it->second;
10551058
if (origtx.GetDepthInMainChain() > 0 || origtx.InMempool()) {
10561059
return false;
10571060
}
@@ -1062,8 +1065,9 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10621065
uint256 now = *todo.begin();
10631066
todo.erase(now);
10641067
done.insert(now);
1065-
assert(mapWallet.count(now));
1066-
CWalletTx& wtx = mapWallet[now];
1068+
auto it = mapWallet.find(now);
1069+
assert(it != mapWallet.end());
1070+
CWalletTx& wtx = it->second;
10671071
int currentconfirm = wtx.GetDepthInMainChain();
10681072
// If the orig tx was not in block, none of its spends can be
10691073
assert(currentconfirm <= 0);
@@ -1088,8 +1092,10 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10881092
// available of the outputs it spends. So force those to be recomputed
10891093
for (const CTxIn& txin : wtx.tx->vin)
10901094
{
1091-
if (mapWallet.count(txin.prevout.hash))
1092-
mapWallet[txin.prevout.hash].MarkDirty();
1095+
auto it = mapWallet.find(txin.prevout.hash);
1096+
if (it != mapWallet.end()) {
1097+
it->second.MarkDirty();
1098+
}
10931099
}
10941100
}
10951101
}
@@ -1127,8 +1133,9 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
11271133
uint256 now = *todo.begin();
11281134
todo.erase(now);
11291135
done.insert(now);
1130-
assert(mapWallet.count(now));
1131-
CWalletTx& wtx = mapWallet[now];
1136+
auto it = mapWallet.find(now);
1137+
assert(it != mapWallet.end());
1138+
CWalletTx& wtx = it->second;
11321139
int currentconfirm = wtx.GetDepthInMainChain();
11331140
if (conflictconfirms < currentconfirm) {
11341141
// Block is 'more conflicted' than current confirm; update.
@@ -1147,10 +1154,11 @@ void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
11471154
}
11481155
// If a transaction changes 'conflicted' state, that changes the balance
11491156
// available of the outputs it spends. So force those to be recomputed
1150-
for (const CTxIn& txin : wtx.tx->vin)
1151-
{
1152-
if (mapWallet.count(txin.prevout.hash))
1153-
mapWallet[txin.prevout.hash].MarkDirty();
1157+
for (const CTxIn& txin : wtx.tx->vin) {
1158+
auto it = mapWallet.find(txin.prevout.hash);
1159+
if (it != mapWallet.end()) {
1160+
it->second.MarkDirty();
1161+
}
11541162
}
11551163
}
11561164
}
@@ -1165,10 +1173,11 @@ void CWallet::SyncTransaction(const CTransactionRef& ptx, const CBlockIndex *pin
11651173
// If a transaction changes 'conflicted' state, that changes the balance
11661174
// available of the outputs it spends. So force those to be
11671175
// recomputed, also:
1168-
for (const CTxIn& txin : tx.vin)
1169-
{
1170-
if (mapWallet.count(txin.prevout.hash))
1171-
mapWallet[txin.prevout.hash].MarkDirty();
1176+
for (const CTxIn& txin : tx.vin) {
1177+
auto it = mapWallet.find(txin.prevout.hash);
1178+
if (it != mapWallet.end()) {
1179+
it->second.MarkDirty();
1180+
}
11721181
}
11731182
}
11741183

0 commit comments

Comments
 (0)