Skip to content

Use wallet UTXOs whenever possible to avoid looping through all wallet txes - #3156

Merged
codablock merged 2 commits into
dashpay:developfrom
UdjinM6:speeduptxesandbalances
Oct 17, 2019
Merged

Use wallet UTXOs whenever possible to avoid looping through all wallet txes#3156
codablock merged 2 commits into
dashpay:developfrom
UdjinM6:speeduptxesandbalances

Conversation

@UdjinM6

@UdjinM6 UdjinM6 commented Oct 16, 2019

Copy link
Copy Markdown

This applies the same idea as #1655 to regular/non-PS wallet functions.

@UdjinM6 UdjinM6 added this to the 14.1 milestone Oct 16, 2019
@codablock

codablock commented Oct 16, 2019

Copy link
Copy Markdown

I'm wondering if this isn't a candidate for a helper method like this:

    struct WalletTxHasher {
        StaticSaltedHasher h;
        size_t operator()(const CWalletTx* a) const {
            return h(a->GetHash());
        }
    };
    std::unordered_set<const CWalletTx*, WalletTxHasher> GetSpendableTXs() const
    {
        std::unordered_set<const CWalletTx*, WalletTxHasher> ret;
        for (auto it = setWalletUTXO.begin(); it != setWalletUTXO.end(); ) {
            const auto& outpoint = *it;
            const auto jt = mapWallet.find(outpoint.hash);
            if (jt != mapWallet.end()) {
                ret.emplace(&jt->second);
            }

            // setWalletUTXO is sorted by COutPoint, which means that all UTXOs for the same TX are neighbors
            // skip entries until we encounter a new TX
            while (it != setWalletUTXO.end() && it->hash == outpoint.hash) {
                ++it;
            }
        }
        return ret;
    }

With this you can change all loops to something like:

for (auto pcoin : GetSpendableTXs()) {
  const uint256& wtxid = pcoin->GetHash();
  ...
}

There should be no overhead in this, as the compiler is able to optimize returning movable objects.

@codablock

codablock commented Oct 16, 2019

Copy link
Copy Markdown

FYI, there was an error in the if (jt == mapWallet.end())...it did not increment it. EDIT Optimized it a little bit more.

@codablock codablock left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

utACK

@codablock
codablock merged commit 3c6b5f9 into dashpay:develop Oct 17, 2019
@UdjinM6
UdjinM6 deleted the speeduptxesandbalances branch November 26, 2020 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants