Skip to content

Commit 7eb7076

Browse files
committed
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
2 parents b62b437 + a59dac3 commit 7eb7076

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,9 +3149,13 @@ UniValue listunspent(const JSONRPCRequest& request)
31493149

31503150
UniValue results(UniValue::VARR);
31513151
std::vector<COutput> vecOutputs;
3152-
LOCK2(cs_main, pwallet->cs_wallet);
3152+
{
3153+
LOCK2(cs_main, pwallet->cs_wallet);
3154+
pwallet->AvailableCoins(vecOutputs, !include_unsafe, nullptr, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
3155+
}
3156+
3157+
LOCK(pwallet->cs_wallet);
31533158

3154-
pwallet->AvailableCoins(vecOutputs, !include_unsafe, nullptr, nMinimumAmount, nMaximumAmount, nMinimumSumAmount, nMaximumCount, nMinDepth, nMaxDepth);
31553159
for (const COutput& out : vecOutputs) {
31563160
CTxDestination address;
31573161
const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey;
@@ -3167,10 +3171,11 @@ UniValue listunspent(const JSONRPCRequest& request)
31673171
if (fValidAddress) {
31683172
entry.pushKV("address", EncodeDestination(address));
31693173

3170-
if (pwallet->mapAddressBook.count(address)) {
3171-
entry.pushKV("label", pwallet->mapAddressBook[address].name);
3174+
auto i = pwallet->mapAddressBook.find(address);
3175+
if (i != pwallet->mapAddressBook.end()) {
3176+
entry.pushKV("label", i->second.name);
31723177
if (IsDeprecatedRPCEnabled("accounts")) {
3173-
entry.pushKV("account", pwallet->mapAddressBook[address].name);
3178+
entry.pushKV("account", i->second.name);
31743179
}
31753180
}
31763181

0 commit comments

Comments
 (0)