Skip to content

Commit be27048

Browse files
author
MarcoFalke
committed
Merge #13241: scripted-diff: Avoid temporary copies when looping over std::map
9b72c98 scripted-diff: Avoid temporary copies when looping over std::map (Ben Woosley) Pull request description: 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. Tree-SHA512: b656d66b69ffa1eb954124aa8ae2bc5436ca50262abefa93bdda55cfcdaffc5ff90cd40539051a2bd06355ba69ddf245265cc8764eebff66d761b3aec06155a9
2 parents 280924e + 9b72c98 commit be27048

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
@@ -632,7 +632,7 @@ static void CleanupBlockRevFiles()
632632
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
633633
// start removing block files.
634634
int nContigCounter = 0;
635-
for (const std::pair<std::string, fs::path>& item : mapBlockFiles) {
635+
for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
636636
if (atoi(item.first) == nContigCounter) {
637637
nContigCounter++;
638638
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
@@ -3837,7 +3837,7 @@ bool CChainState::LoadBlockIndex(const Consensus::Params& consensus_params, CBlo
38373837
// Calculate nChainWork
38383838
std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
38393839
vSortedByHeight.reserve(mapBlockIndex.size());
3840-
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3840+
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
38413841
{
38423842
CBlockIndex* pindex = item.second;
38433843
vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
@@ -3904,7 +3904,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
39043904
// Check presence of blk files
39053905
LogPrintf("Checking all blk files are present...\n");
39063906
std::set<int> setBlkDataFiles;
3907-
for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3907+
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
39083908
{
39093909
CBlockIndex* pindex = item.second;
39103910
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;
@@ -1527,7 +1527,7 @@ static UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bo
15271527

15281528
// Tally
15291529
std::map<CTxDestination, tallyitem> mapTally;
1530-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
1530+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
15311531
const CWalletTx& wtx = pairWtx.second;
15321532

15331533
if (wtx.IsCoinBase() || !CheckFinalTx(*wtx.tx))
@@ -2106,13 +2106,13 @@ static UniValue listaccounts(const JSONRPCRequest& request)
21062106
includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
21072107

21082108
std::map<std::string, CAmount> mapAccountBalances;
2109-
for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
2109+
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
21102110
if (IsMine(*pwallet, entry.first) & includeWatchonly) { // This address belongs to me
21112111
mapAccountBalances[entry.second.name] = 0;
21122112
}
21132113
}
21142114

2115-
for (const std::pair<uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
2115+
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
21162116
const CWalletTx& wtx = pairWtx.second;
21172117
CAmount nFee;
21182118
std::string strSentAccount;
@@ -2141,7 +2141,7 @@ static UniValue listaccounts(const JSONRPCRequest& request)
21412141
mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
21422142

21432143
UniValue ret(UniValue::VOBJ);
2144-
for (const std::pair<std::string, CAmount>& accountBalance : mapAccountBalances) {
2144+
for (const std::pair<const std::string, CAmount>& accountBalance : mapAccountBalances) {
21452145
ret.pushKV(accountBalance.first, ValueFromAmount(accountBalance.second));
21462146
}
21472147
return ret;
@@ -2250,7 +2250,7 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
22502250

22512251
UniValue transactions(UniValue::VARR);
22522252

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

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

41884188
// Find all addresses that have the given label
41894189
UniValue ret(UniValue::VOBJ);
4190-
for (const std::pair<CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
4190+
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
41914191
if (item.second.name == label) {
41924192
ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
41934193
}
@@ -4240,7 +4240,7 @@ static UniValue listlabels(const JSONRPCRequest& request)
42404240

42414241
// Add to a set to sort by label name, then insert into Univalue array
42424242
std::set<std::string> label_set;
4243-
for (const std::pair<CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
4243+
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->mapAddressBook) {
42444244
if (purpose.empty() || entry.second.purpose == purpose) {
42454245
label_set.insert(entry.second.name);
42464246
}

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)