Merge #12639: Reduce cs_main lock in listunspent

a59dac3 refactor: Avoid extra lookups of mapAddressBook in listunspent RPC (João Barbosa)
d76962e rpc: Reduce cs_main lock in listunspent (João Barbosa)

Pull request description:

  On my system, where the wallet has 10000 unspents, the `cs_main` lock duration changed from 191ms to 36ms. The loop that generates the response takes around 155ms. So, the lock duration is reduced to around 20%.

Tree-SHA512: ddaae591f39da59a9d1a8e9ffe773d857687789476f566ca273d310ad531da6dacff80cac69f3334c601c251ac7c5ed4136656c725aa3d611c6bbf734111946e
This commit is contained in:
Wladimir J. van der Laan 2018-05-03 14:43:19 +02:00 committed by UdjinM6
parent a03d649a48
commit dcd6c1df3c
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9

View File

@ -3623,9 +3623,13 @@ UniValue listunspent(const JSONRPCRequest& request)
UniValue results(UniValue::VARR); UniValue results(UniValue::VARR);
std::vector<COutput> vecOutputs; std::vector<COutput> vecOutputs;
LOCK2(cs_main, pwallet->cs_wallet); {
LOCK2(cs_main, pwallet->cs_wallet);
pwallet->AvailableCoins(vecOutputs, !include_unsafe, &coinControl, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
}
LOCK(pwallet->cs_wallet);
pwallet->AvailableCoins(vecOutputs, !include_unsafe, &coinControl, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
for (const COutput& out : vecOutputs) { for (const COutput& out : vecOutputs) {
CTxDestination address; CTxDestination address;
const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey; const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
@ -3641,10 +3645,11 @@ UniValue listunspent(const JSONRPCRequest& request)
if (fValidAddress) { if (fValidAddress) {
entry.pushKV("address", EncodeDestination(address)); entry.pushKV("address", EncodeDestination(address));
if (pwallet->mapAddressBook.count(address)) { auto i = pwallet->mapAddressBook.find(address);
entry.pushKV("label", pwallet->mapAddressBook[address].name); if (i != pwallet->mapAddressBook.end()) {
entry.pushKV("label", i->second.name);
if (IsDeprecatedRPCEnabled("accounts")) { if (IsDeprecatedRPCEnabled("accounts")) {
entry.pushKV("account", pwallet->mapAddressBook[address].name); entry.pushKV("account", i->second.name);
} }
} }