Merge #12330: Reduce scope of cs_main and cs_wallet locks in listtransactions

c409b1adac [rpc] Reduce scope of cs_main and cs_wallet locks in listtransactions (João Barbosa)

Pull request description:

  Trivial change, no behaviour change.

  Benchmark done as follow:
   - run with `-regtest`
   - wallet with 5000 transactions
   - measured the time spent with the lock and the total time
   - times are an average of 100 `listtransactions --count=...` calls

  | `--count` | lock (ms) | total (ms) | saving |
  |--:|--:|--:|--:|
  | 10 | 0.2230 | 0.2510 | 11% |
  | 100 | 2.5150 | 2.8690 | 12% |
  | 1000 | 20.0320 | 23.3490 | 14% |
  | 10000 | 105.2070 | 125.5310 | 16% |

Tree-SHA512: ebedfeeb4c8ad75c89128e53cae976a82967dbb5ffd129da0f7204ccf9c3c15070b3d509f3767bebd745512e410200cc546147c836e82409f95fc9b8d14fc3ed
This commit is contained in:
MarcoFalke 2018-02-05 16:16:53 -05:00
commit 2a30e67d20
No known key found for this signature in database
GPG Key ID: D2EA4850E7528B25
1 changed files with 16 additions and 13 deletions

View File

@ -1794,8 +1794,6 @@ UniValue listtransactions(const JSONRPCRequest& request)
// the user could have gotten from another RPC command prior to now
pwallet->BlockUntilSyncedToCurrentChain();
LOCK2(cs_main, pwallet->cs_wallet);
std::string strAccount = "*";
if (!request.params[0].isNull())
strAccount = request.params[0].get_str();
@ -1817,20 +1815,25 @@ UniValue listtransactions(const JSONRPCRequest& request)
UniValue ret(UniValue::VARR);
const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
// iterate backwards until we have nCount items to return:
for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
{
CWalletTx *const pwtx = (*it).second.first;
if (pwtx != nullptr)
ListTransactions(pwallet, *pwtx, strAccount, 0, true, ret, filter);
CAccountingEntry *const pacentry = (*it).second.second;
if (pacentry != nullptr)
AcentryToJSON(*pacentry, strAccount, ret);
LOCK2(cs_main, pwallet->cs_wallet);
if ((int)ret.size() >= (nCount+nFrom)) break;
const CWallet::TxItems & txOrdered = pwallet->wtxOrdered;
// iterate backwards until we have nCount items to return:
for (CWallet::TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
{
CWalletTx *const pwtx = (*it).second.first;
if (pwtx != nullptr)
ListTransactions(pwallet, *pwtx, strAccount, 0, true, ret, filter);
CAccountingEntry *const pacentry = (*it).second.second;
if (pacentry != nullptr)
AcentryToJSON(*pacentry, strAccount, ret);
if ((int)ret.size() >= (nCount+nFrom)) break;
}
}
// ret is newest to oldest
if (nFrom > (int)ret.size())