Skip to content

Commit 9b72c98

Browse files
committed
scripted-diff: Avoid temporary copies when looping over std::map
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-
1 parent 7c32b41 commit 9b72c98

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ static void CleanupBlockRevFiles()
614614
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
615615
// start removing block files.
616616
int nContigCounter = 0;
617-
for (const std::pair<std::string, fs::path>& item : mapBlockFiles) {
617+
for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
618618
if (atoi(item.first) == nContigCounter) {
619619
nContigCounter++;
620620
continue;

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
475475
UniValue localAddresses(UniValue::VARR);
476476
{
477477
LOCK(cs_mapLocalHost);
478-
for (const std::pair<CNetAddr, LocalServiceInfo> &item : mapLocalHost)
478+
for (const std::pair<const CNetAddr, LocalServiceInfo> &item : mapLocalHost)
479479
{
480480
UniValue rec(UniValue::VOBJ);
481481
rec.pushKV("address", item.first.ToString());

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,7 +3840,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
38403840
// Calculate nChainWork
38413841
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
38423842
vSortedByHeight.reserve(mapBlockIndex.size());
3843-
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3843+
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
38443844
{
38453845
CBlockIndex* pindex = item.second;
38463846
vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
@@ -3907,7 +3907,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
39073907
// Check presence of blk files
39083908
LogPrintf("Checking all blk files are present...\n");
39093909
std::set<int> setBlkDataFiles;
3910-
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3910+
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
39113911
{
39123912
CBlockIndex* pindex = item.second;
39133913
if (pindex->nStatus & BLOCK_HAVE_DATA) {

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
120120
}
121121
entry.pushKV("bip125-replaceable", rbfStatus);
122122

123-
for (const std::pair<std::string, std::string>& item : wtx.mapValue)
123+
for (const std::pair<const std::string, std::string>& item : wtx.mapValue)
124124
entry.pushKV(item.first, item.second);
125125
}
126126

@@ -343,7 +343,7 @@ static UniValue setlabel(const JSONRPCRequest& request)
343343
// If so, delete the account record for it. Labels, unlike addresses, can be deleted,
344344
// and if we wouldn't do this, the record would stick around forever.
345345
bool found_address = false;
346-
for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
346+
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
347347
if (item.second.name == label) {
348348
found_address = true;
349349
break;
@@ -440,7 +440,7 @@ static UniValue getaddressesbyaccount(const JSONRPCRequest& request)
440440

441441
// Find all addresses that have the given account
442442
UniValue ret(UniValue::VARR);
443-
for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
443+
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
444444
const CTxDestination& dest = item.first;
445445
const std::string& strName = item.second.name;
446446
if (strName == strAccount) {
@@ -753,7 +753,7 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
753753

754754
// Tally
755755
CAmount nAmount = 0;
756-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
756+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
757757
const CWalletTx& wtx = pairWtx.second;
758758
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
759759
continue;
@@ -821,7 +821,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
821821

822822
// Tally
823823
CAmount nAmount = 0;
824-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
824+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
825825
const CWalletTx& wtx = pairWtx.second;
826826
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
827827
continue;
@@ -1534,7 +1534,7 @@ static UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bo
15341534

15351535
// Tally
15361536
std::map<CTxDestination, tallyitem> mapTally;
1537-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1537+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
15381538
const CWalletTx& wtx = pairWtx.second;
15391539

15401540
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
@@ -2113,13 +2113,13 @@ static UniValue listaccounts(const JSONRPCRequest& request)
21132113
includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
21142114

21152115
std::map<std::string, CAmount> mapAccountBalances;
2116-
for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
2116+
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
21172117
if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
21182118
mapAccountBalances[entry.second.name] = 0;
21192119
}
21202120
}
21212121

2122-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
2122+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
21232123
const CWalletTx& wtx = pairWtx.second;
21242124
CAmount nFee;
21252125
std::string strSentAccount;
@@ -2148,7 +2148,7 @@ static UniValue listaccounts(const JSONRPCRequest& request)
21482148
mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
21492149

21502150
UniValue ret(UniValue::VOBJ);
2151-
for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) {
2151+
for (const std::pair<const std::string, CAmount>& accountBalance : mapAccountBalances) {
21522152
ret.pushKV(accountBalance.first, ValueFromAmount(accountBalance.second));
21532153
}
21542154
return ret;
@@ -2257,7 +2257,7 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
22572257

22582258
UniValue transactions(UniValue::VARR);
22592259

2260-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
2260+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
22612261
CWalletTx tx = pairWtx.second;
22622262

22632263
if (depth == -1 || tx.GetDepthInMainChain() < depth) {
@@ -4194,7 +4194,7 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
41944194

41954195
// Find all addresses that have the given label
41964196
UniValue ret(UniValue::VOBJ);
4197-
for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
4197+
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
41984198
if (item.second.name == label) {
41994199
ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
42004200
}
@@ -4247,7 +4247,7 @@ static UniValue listlabels(const JSONRPCRequest& request)
42474247

42484248
// Add to a set to sort by label name, then insert into Univalue array
42494249
std::set<std::string> label_set;
4250-
for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
4250+
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
42514251
if (purpose.empty() || entry.second.purpose == purpose) {
42524252
label_set.insert(entry.second.name);
42534253
}

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,7 +3284,7 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
32843284

32853285
// Delete destdata tuples associated with address
32863286
std::string strAddress = EncodeDestination(address);
3287-
for (const std::pair<std::string, std::string> &item : mapAddressBook[address].destdata)
3287+
for (const std::pair<const std::string, std::string> &item : mapAddressBook[address].destdata)
32883288
{
32893289
WalletBatch(*database).EraseDestData(strAddress, item.first);
32903290
}
@@ -3685,7 +3685,7 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
36853685
{
36863686
LOCK(cs_wallet);
36873687
std::set<CTxDestination> result;
3688-
for (const std::pair<CTxDestination, CAddressBookData>& item : mapAddressBook)
3688+
for (const std::pair<const CTxDestination, CAddressBookData>& item : mapAddressBook)
36893689
{
36903690
const CTxDestination& address = item.first;
36913691
const std::string& strName = item.second.name;

0 commit comments

Comments
 (0)