-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. #13249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
How about also including the range-for changes from #12158? |
@Empact I think it better to do (a subset of) the #12158 changes in follow-up PRs to keep this PR as mechanical and easy-to-review as possible. Basically what @laanwj suggested in #12158 (comment) :-) This PR should hopefully be trivial to review :-) |
Yeah, I like it as-is, just suggesting since the Anyway, you have my Tested ACK 3299ed7. |
Rebased! |
utACK 0143466 |
utACK 0143466 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only made some comments where I think the iterated type can be const &
. Can you review the whole change if you agree with such change?
src/miner.cpp
Outdated
@@ -245,7 +245,7 @@ int BlockAssembler::UpdatePackagesForAdded(const CTxMemPool::setEntries& already | |||
CTxMemPool::setEntries descendants; | |||
mempool.CalculateDescendants(it, descendants); | |||
// Insert all descendants (not yet in block) into the modified set | |||
for (CTxMemPool::txiter desc : descendants) { | |||
for (const CTxMemPool::txiter desc : descendants) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const CTxMemPool::txiter&
?
src/net.cpp
Outdated
@@ -1603,7 +1603,7 @@ void CConnman::ThreadDNSAddressSeed() | |||
|
|||
LOCK(cs_vNodes); | |||
int nRelevant = 0; | |||
for (auto pnode : vNodes) { | |||
for (const auto pnode : vNodes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, s/auto/CNode*?
src/net_processing.cpp
Outdated
@@ -850,7 +850,7 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb | |||
// Erase orphan transactions included or precluded by this block | |||
if (vOrphanErase.size()) { | |||
int nErased = 0; | |||
for (uint256 &orphanHash : vOrphanErase) { | |||
for (const uint256 &orphanHash : vOrphanErase) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const uint256&
?
src/net_processing.cpp
Outdated
@@ -2272,7 +2272,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr | |||
} | |||
} | |||
|
|||
for (uint256 hash : vEraseQueue) | |||
for (const uint256 hash : vEraseQueue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
3e16523
to
9a4655f
Compare
@promag Thanks for reviewing! Feedback addressed. Please re-review :-) |
src/policy/rbf.cpp
Outdated
@@ -38,7 +38,7 @@ RBFTransactionState IsRBFOptIn(const CTransaction &tx, CTxMemPool &pool) | |||
CTxMemPoolEntry entry = *pool.mapTx.find(tx.GetHash()); | |||
pool.CalculateMemPoolAncestors(entry, setAncestors, noLimit, noLimit, noLimit, noLimit, dummy, false); | |||
|
|||
for (CTxMemPool::txiter it : setAncestors) { | |||
for (const CTxMemPool::txiter it : setAncestors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could be a reference too for consistency with src/miner.cpp
.
src/qt/bitcoin.cpp
Outdated
@@ -442,7 +442,7 @@ void BitcoinApplication::requestShutdown() | |||
|
|||
#ifdef ENABLE_WALLET | |||
window->removeAllWallets(); | |||
for (WalletModel *walletModel : m_wallet_models) { | |||
for (const WalletModel *walletModel : m_wallet_models) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what whitespace change you're suggesting? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just that the developer-notes prefer WalletModel*
. Fine to ignore.
src/rpc/blockchain.cpp
Outdated
@@ -532,14 +532,14 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request) | |||
|
|||
if (!fVerbose) { | |||
UniValue o(UniValue::VARR); | |||
for (CTxMemPool::txiter ancestorIt : setAncestors) { | |||
for (const CTxMemPool::txiter ancestorIt : setAncestors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Same for this file
src/txmempool.cpp
Outdated
@@ -88,7 +88,7 @@ void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendan | |||
int64_t modifySize = 0; | |||
CAmount modifyFee = 0; | |||
int64_t modifyCount = 0; | |||
for (txiter cit : setAllDescendants) { | |||
for (const txiter cit : setAllDescendants) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Same
src/wallet/wallet.cpp
Outdated
@@ -3210,7 +3210,7 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256 | |||
{ | |||
AssertLockHeld(cs_wallet); // mapWallet | |||
DBErrors nZapSelectTxRet = WalletBatch(*database,"cr+").ZapSelectTx(vHashIn, vHashOut); | |||
for (uint256 hash : vHashOut) | |||
for (const uint256 hash : vHashOut) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Reference?
src/wallet/walletdb.cpp
Outdated
@@ -610,7 +610,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) | |||
if ((wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) | |||
pwallet->UpdateTimeFirstKey(1); | |||
|
|||
for (uint256 hash : wss.vWalletUpgrade) | |||
for (const uint256 hash : wss.vWalletUpgrade) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: References?
All nits, just calling out handling where inconsistent. |
utACK 9a4655fd15e632d95651e4681936f8ea13457ae1 These changes should be obviously safe. Adding |
9a4655f
to
2b132fe
Compare
ea2cd74
to
6a980ed
Compare
Rebased! :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments regarding auto
usage.
src/wallet/wallet.cpp
Outdated
@@ -2390,7 +2390,7 @@ std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const | |||
LOCK2(cs_main, cs_wallet); | |||
AvailableCoins(availableCoins); | |||
|
|||
for (auto& coin : availableCoins) { | |||
for (const auto& coin : availableCoins) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use const COutput& coin
?
src/validation.cpp
Outdated
@@ -4493,7 +4493,7 @@ void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams) | |||
|
|||
// Build forward-pointing map of the entire block tree. | |||
std::multimap<CBlockIndex*,CBlockIndex*> forward; | |||
for (auto& entry : mapBlockIndex) { | |||
for (const auto& entry : mapBlockIndex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most common usage when iterating mapBlockIndex
is
for (const std::pair<const uint256, CBlockIndex*>& item : mapBlockIndex)
And there is also the above:
for (const BlockMap::value_type& entry : mapBlockIndex)
(I prefer the 1st).
src/wallet/wallet.cpp
Outdated
@@ -1570,7 +1570,7 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wall | |||
// Look up the inputs. We should have already checked that this transaction | |||
// IsAllFromMe(ISMINE_SPENDABLE), so every input should already be in our | |||
// wallet, with a valid index into the vout array, and the ability to sign. | |||
for (auto& input : tx.vin) { | |||
for (const auto& input : tx.vin) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use const CTxIn& input
(most common usage).
279caa6
to
ddd0045
Compare
@promag Thanks for reviewing. Feedback addressed. Please re-review :-) |
…ssary copying of objects in range declarations.
ddd0045
to
f34c8c4
Compare
Rebased! Please re-review :-) |
utACK f34c8c4 |
…. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
…ssary copying of objects in range declarations. Summary: Make objects in range declarations immutable by default. Backport of Bitcoin Core PR13249 bitcoin/bitcoin#13249 (D4191 done again) Test Plan: ``` make check-all ``` Reviewers: Fabien, #bitcoin_abc, deadalnix Reviewed By: #bitcoin_abc, deadalnix Differential Revision: https://reviews.bitcoinabc.org/D4221
…default. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
…default. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
…default. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
…default. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
…default. Avoid unnecessary copying of objects in range declarations. f34c8c4 Make objects in range declarations immutable by default. Avoid unnecessary copying of objects in range declarations. (practicalswift) Pull request description: Make objects in range declarations immutable by default. Rationale: * Immutable objects are easier to reason about. * Prevents accidental or hard-to-notice change of value. Tree-SHA512: cad69d35f0cf8a938b848e65dd537c621d96fe3369be306b65ef0cd1baf6cc0a9f28bc230e1e383d810c555a6743d08cb6b2b0bd51856d4611f537a12e5abb8b
Make objects in range declarations immutable by default.
Rationale: