Skip to content

Commit c5fa7ed

Browse files
committed
Merge bitcoin/bitcoin#25544: wallet: don't iter twice when getting the cached debit/credit amount
757216e wallet: don't iter twice when getting the cached debit/credit amount (Antoine Poinsot) Pull request description: A small optimization i stumbled upon while looking at something else. Figured it could be worth a PR. Instead of calling GetCachableAmount twice, which will result in iterating through all the transaction txins/txouts and calling GetDebit/GetCredit (which lock cs_wallet), just merge the filters and do it once. ACKs for top commit: achow101: ACK 757216e aureleoules: ACK 757216e. Tree-SHA512: 0dbbdd24231380196e929dce572752e6be1d69457252a7215e279e71d6199483b516f64019ae999a91dbce7fdd86f8bf0336b6e151cca93cbcf51bc854e838a2
2 parents 4e2929e + 757216e commit c5fa7ed

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/wallet/receive.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,10 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
130130
return 0;
131131

132132
CAmount credit = 0;
133-
if (filter & ISMINE_SPENDABLE) {
133+
const isminefilter get_amount_filter{filter & ISMINE_ALL};
134+
if (get_amount_filter) {
134135
// GetBalance can assume transactions in mapWallet won't change
135-
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_SPENDABLE);
136-
}
137-
if (filter & ISMINE_WATCH_ONLY) {
138-
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_WATCH_ONLY);
136+
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, get_amount_filter);
139137
}
140138
return credit;
141139
}
@@ -146,11 +144,9 @@ CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const ismi
146144
return 0;
147145

148146
CAmount debit = 0;
149-
if (filter & ISMINE_SPENDABLE) {
150-
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_SPENDABLE);
151-
}
152-
if (filter & ISMINE_WATCH_ONLY) {
153-
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_WATCH_ONLY);
147+
const isminefilter get_amount_filter{filter & ISMINE_ALL};
148+
if (get_amount_filter) {
149+
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, get_amount_filter);
154150
}
155151
return debit;
156152
}

0 commit comments

Comments
 (0)