wallet: don't iter twice when getting the cached debit/credit amount #25544

pull darosior wants to merge 1 commits into bitcoin:master from darosior:wallet_needless_iteration changing 1 files +6 −10
  1. darosior commented at 1:59 PM on July 5, 2022: member

    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.

  2. wallet: don't iter twice when getting the cached debit/credit amount
    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.
    757216e31c
  3. laanwj added the label Wallet on Jul 5, 2022
  4. aureleoules commented at 3:28 PM on July 7, 2022: member

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

    <details> <summary>Small suggestion</summary>

    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);
    

    </details>

  5. achow101 commented at 5:28 PM on July 8, 2022: member

    ACK 757216e31cac7dcd45e11b2a2c6148420b3b99da

  6. fanquake merged this on Jul 18, 2022
  7. fanquake closed this on Jul 18, 2022

  8. sidhujag referenced this in commit d70875835e on Jul 18, 2022
  9. bitcoin locked this on Jul 18, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 15:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me