Skip to content

Conversation

@darosior
Copy link
Member

@darosior darosior commented Jul 5, 2022

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.

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.
@laanwj laanwj added the Wallet label Jul 5, 2022
Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 757216e.
Neat trick. I verified that the behavior does not change.

Small suggestion
diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp
index d3303c0b1..3df4adf03 100644
--- a/src/wallet/receive.cpp
+++ b/src/wallet/receive.cpp
@@ -129,7 +129,7 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
     if (wallet.IsTxImmatureCoinBase(wtx))
         return 0;
 
-    CAmount credit = 0;
+    CAmount credit{0};
     const isminefilter get_amount_filter{filter & ISMINE_ALL};
     if (get_amount_filter) {
         // GetBalance can assume transactions in mapWallet won't change
@@ -143,7 +143,7 @@ CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const ismi
     if (wtx.tx->vin.empty())
         return 0;
 
-    CAmount debit = 0;
+    CAmount debit{0};
     const isminefilter get_amount_filter{filter & ISMINE_ALL};
     if (get_amount_filter) {
         debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, get_amount_filter);

@achow101
Copy link
Member

achow101 commented Jul 8, 2022

ACK 757216e

@fanquake fanquake merged commit c5fa7ed into bitcoin:master Jul 18, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 18, 2022
… 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
@bitcoin bitcoin locked and limited conversation to collaborators Jul 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants