scripted-diff: Avoid temporary copies when looping over std::map (Merge #13241)

The ::value_type of the std::map/std::multimap/std::unordered_map containers is
std::pair<const Key, T>. Dropping the const results in an unnecessary copy,
for example in C++11 range-based loops.

For this I started with a more general scripted diff, then narrowed it down
based on the inspection showing that all actual map/multimap/unordered_map
variables used in loops start with m or have map in the name.

-BEGIN VERIFY SCRIPT-
sed -i -E 's/for \(([^<]*)std::pair<([^c])(.+) : m/for (\1std::pair<const \2\3 : m/' src/*.cpp src/**/*.cpp
sed -i -E 's/for \(([^<]*)std::pair<([^c])(.+) : (.*)map/for (\1std::pair<const \2\3 : \4map/' src/*.cpp src/**/*.cpp
-END VERIFY SCRIPT-

Signed-off-by: pasta <pasta@dashboost.org>
This commit is contained in:
pasta 2020-07-07 11:46:21 -05:00
parent ae16904b51
commit 371e213067
8 changed files with 19 additions and 19 deletions

View File

@ -757,7 +757,7 @@ void CleanupBlockRevFiles()
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist) // keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
// start removing block files. // start removing block files.
int nContigCounter = 0; int nContigCounter = 0;
for (const std::pair<std::string, fs::path>& item : mapBlockFiles) { for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
if (atoi(item.first) == nContigCounter) { if (atoi(item.first) == nContigCounter) {
nContigCounter++; nContigCounter++;
continue; continue;

View File

@ -79,7 +79,7 @@ public:
cachedAddressTable.clear(); cachedAddressTable.clear();
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
for (const std::pair<CTxDestination, CAddressBookData>& item : wallet->mapAddressBook) for (const std::pair<const CTxDestination, CAddressBookData>& item : wallet->mapAddressBook)
{ {
const CTxDestination& address = item.first; const CTxDestination& address = item.first;
bool fMine = IsMine(*wallet, address); bool fMine = IsMine(*wallet, address);

View File

@ -667,7 +667,7 @@ void CoinControlDialog::updateView()
std::map<QString, std::vector<COutput> > mapCoins; std::map<QString, std::vector<COutput> > mapCoins;
model->listCoins(mapCoins); model->listCoins(mapCoins);
for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) { for (const std::pair<const QString, std::vector<COutput>>& coins : mapCoins) {
CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem(); CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked); itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
QString sWalletAddress = coins.first; QString sWalletAddress = coins.first;

View File

@ -501,7 +501,7 @@ UniValue getnetworkinfo(const JSONRPCRequest& request)
UniValue localAddresses(UniValue::VARR); UniValue localAddresses(UniValue::VARR);
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
for (const std::pair<CNetAddr, LocalServiceInfo> &item : mapLocalHost) for (const std::pair<const CNetAddr, LocalServiceInfo> &item : mapLocalHost)
{ {
UniValue rec(UniValue::VOBJ); UniValue rec(UniValue::VOBJ);
rec.pushKV("address", item.first.ToString()); rec.pushKV("address", item.first.ToString());

View File

@ -72,7 +72,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
// Scan templates // Scan templates
const CScript& script1 = scriptPubKey; const CScript& script1 = scriptPubKey;
for (const std::pair<txnouttype, CScript>& tplate : mTemplates) for (const std::pair<const txnouttype, CScript>& tplate : mTemplates)
{ {
const CScript& script2 = tplate.second; const CScript& script2 = tplate.second;
vSolutionsRet.clear(); vSolutionsRet.clear();

View File

@ -4060,7 +4060,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
// Calculate nChainWork // Calculate nChainWork
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight; std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
vSortedByHeight.reserve(mapBlockIndex.size()); vSortedByHeight.reserve(mapBlockIndex.size());
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex) for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
{ {
CBlockIndex* pindex = item.second; CBlockIndex* pindex = item.second;
vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex)); vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
@ -4132,7 +4132,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
// Check presence of blk files // Check presence of blk files
LogPrintf("Checking all blk files are present...\n"); LogPrintf("Checking all blk files are present...\n");
std::set<int> setBlkDataFiles; std::set<int> setBlkDataFiles;
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex) for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
{ {
CBlockIndex* pindex = item.second; CBlockIndex* pindex = item.second;
if (pindex->nStatus & BLOCK_HAVE_DATA) { if (pindex->nStatus & BLOCK_HAVE_DATA) {

View File

@ -118,7 +118,7 @@ void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
entry.pushKV("time", wtx.GetTxTime()); entry.pushKV("time", wtx.GetTxTime());
entry.pushKV("timereceived", (int64_t)wtx.nTimeReceived); entry.pushKV("timereceived", (int64_t)wtx.nTimeReceived);
for (const std::pair<std::string, std::string>& item : wtx.mapValue) for (const std::pair<const std::string, std::string>& item : wtx.mapValue)
entry.pushKV(item.first, item.second); entry.pushKV(item.first, item.second);
} }
@ -371,7 +371,7 @@ UniValue getaddressesbyaccount(const JSONRPCRequest& request)
// Find all addresses that have the given account // Find all addresses that have the given account
UniValue ret(UniValue::VARR); UniValue ret(UniValue::VARR);
for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) { for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
const CTxDestination& dest = item.first; const CTxDestination& dest = item.first;
const std::string& strName = item.second.name; const std::string& strName = item.second.name;
if (strName == strAccount) { if (strName == strAccount) {
@ -732,7 +732,7 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
// Tally // Tally
CAmount nAmount = 0; CAmount nAmount = 0;
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) { for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
const CWalletTx& wtx = pairWtx.second; const CWalletTx& wtx = pairWtx.second;
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx)) if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
continue; continue;
@ -793,7 +793,7 @@ UniValue getreceivedbyaccount(const JSONRPCRequest& request)
// Tally // Tally
CAmount nAmount = 0; CAmount nAmount = 0;
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) { for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
const CWalletTx& wtx = pairWtx.second; const CWalletTx& wtx = pairWtx.second;
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx)) if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
continue; continue;
@ -1305,7 +1305,7 @@ UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByA
// Tally // Tally
std::map<CTxDestination, tallyitem> mapTally; std::map<CTxDestination, tallyitem> mapTally;
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) { for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
const CWalletTx& wtx = pairWtx.second; const CWalletTx& wtx = pairWtx.second;
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx)) if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
@ -1825,13 +1825,13 @@ UniValue listaccounts(const JSONRPCRequest& request)
includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY; includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
std::map<std::string, CAmount> mapAccountBalances; std::map<std::string, CAmount> mapAccountBalances;
for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) { for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
mapAccountBalances[entry.second.name] = 0; mapAccountBalances[entry.second.name] = 0;
} }
} }
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) { for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
const CWalletTx& wtx = pairWtx.second; const CWalletTx& wtx = pairWtx.second;
CAmount nFee; CAmount nFee;
std::string strSentAccount; std::string strSentAccount;
@ -1860,7 +1860,7 @@ UniValue listaccounts(const JSONRPCRequest& request)
mapAccountBalances[entry.strAccount] += entry.nCreditDebit; mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) { for (const std::pair<const std::string, CAmount>& accountBalance : mapAccountBalances) {
ret.pushKV(accountBalance.first, ValueFromAmount(accountBalance.second)); ret.pushKV(accountBalance.first, ValueFromAmount(accountBalance.second));
} }
return ret; return ret;
@ -1969,7 +1969,7 @@ UniValue listsinceblock(const JSONRPCRequest& request)
UniValue transactions(UniValue::VARR); UniValue transactions(UniValue::VARR);
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) { for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
CWalletTx tx = pairWtx.second; CWalletTx tx = pairWtx.second;
if (depth == -1 || tx.GetDepthInMainChain() < depth) { if (depth == -1 || tx.GetDepthInMainChain() < depth) {

View File

@ -1477,7 +1477,7 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
// the previous block's coinbase, just in case it is ours, so // the previous block's coinbase, just in case it is ours, so
// that we can notify the UI that it should now be displayed. // that we can notify the UI that it should now be displayed.
if (pindex->pprev) { if (pindex->pprev) {
for (const std::pair<uint256, CWalletTx>& p : mapWallet) { for (const std::pair<const uint256, CWalletTx>& p : mapWallet) {
if (p.second.IsCoinBase() && p.second.hashBlock == pindex->pprev->GetBlockHash()) { if (p.second.IsCoinBase() && p.second.hashBlock == pindex->pprev->GetBlockHash()) {
NotifyTransactionChanged(this, p.first, CT_UPDATED); NotifyTransactionChanged(this, p.first, CT_UPDATED);
break; break;
@ -4284,7 +4284,7 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
// Delete destdata tuples associated with address // Delete destdata tuples associated with address
std::string strAddress = EncodeDestination(address); std::string strAddress = EncodeDestination(address);
for (const std::pair<std::string, std::string> &item : mapAddressBook[address].destdata) for (const std::pair<const std::string, std::string> &item : mapAddressBook[address].destdata)
{ {
WalletBatch(*database).EraseDestData(strAddress, item.first); WalletBatch(*database).EraseDestData(strAddress, item.first);
} }
@ -4693,7 +4693,7 @@ std::set<CTxDestination> CWallet::GetAccountAddresses(const std::string& strAcco
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
std::set<CTxDestination> result; std::set<CTxDestination> result;
for (const std::pair<CTxDestination, CAddressBookData>& item : mapAddressBook) for (const std::pair<const CTxDestination, CAddressBookData>& item : mapAddressBook)
{ {
const CTxDestination& address = item.first; const CTxDestination& address = item.first;
const std::string& strName = item.second.name; const std::string& strName = item.second.name;