Skip to content

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented May 16, 2018

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.

@Empact
Copy link
Contributor

Empact commented May 17, 2018

How about also including the range-for changes from #12158?

@practicalswift
Copy link
Contributor Author

practicalswift commented May 17, 2018

@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 :-)

@Empact
Copy link
Contributor

Empact commented May 17, 2018

Yeah, I like it as-is, just suggesting since the & additions wouldn't add much if anything to the line count, so it could be viewed as a form of compression.

Anyway, you have my Tested ACK 3299ed7.
make, make check and test/test_bitcoin ran green locally.

@practicalswift
Copy link
Contributor Author

Rebased!

@ken2812221
Copy link
Contributor

utACK 0143466

@Empact
Copy link
Contributor

Empact commented Jun 10, 2018

utACK 0143466

Copy link
Contributor

@promag promag left a 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) {
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, s/auto/CNode*?

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const uint256&?

@@ -2272,7 +2272,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
}
}

for (uint256 hash : vEraseQueue)
for (const uint256 hash : vEraseQueue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@practicalswift
Copy link
Contributor Author

@promag Thanks for reviewing! Feedback addressed. Please re-review :-)

@@ -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) {
Copy link
Contributor

@Empact Empact Jun 14, 2018

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.

@@ -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) {
Copy link
Contributor

@Empact Empact Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Whitespace

Copy link
Contributor Author

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? :-)

Copy link
Contributor

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.

@@ -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) {
Copy link
Contributor

@Empact Empact Jun 14, 2018

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

@@ -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) {
Copy link
Contributor

@Empact Empact Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Same

@@ -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)
Copy link
Contributor

@Empact Empact Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Reference?

@@ -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)
Copy link
Contributor

@Empact Empact Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: References?

@Empact
Copy link
Contributor

Empact commented Jun 14, 2018

All nits, just calling out handling where inconsistent.

@sipa
Copy link
Member

sipa commented Jun 14, 2018

utACK 9a4655fd15e632d95651e4681936f8ea13457ae1

These changes should be obviously safe. Adding const to variable declarations should never introduce problems (as long as the result compiles). The same is true for converting const variables to references.

@practicalswift
Copy link
Contributor Author

Rebased! :-)

Copy link
Contributor

@promag promag left a 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.

@@ -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) {
Copy link
Contributor

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?

@@ -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) {
Copy link
Contributor

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).

@@ -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) {
Copy link
Contributor

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).

@practicalswift practicalswift force-pushed the for-const branch 2 times, most recently from 279caa6 to ddd0045 Compare July 12, 2018 16:54
@practicalswift
Copy link
Contributor Author

@promag Thanks for reviewing. Feedback addressed. Please re-review :-)

…ssary copying of objects in range declarations.
@practicalswift
Copy link
Contributor Author

Rebased! Please re-review :-)

@ken2812221
Copy link
Contributor

utACK f34c8c4

@laanwj laanwj merged commit f34c8c4 into bitcoin:master Sep 4, 2018
laanwj added a commit that referenced this pull request Sep 4, 2018
…. 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
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2019
…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
@practicalswift practicalswift deleted the for-const branch April 10, 2021 19:35
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 19, 2021
…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
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 19, 2021
…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
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 19, 2021
…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
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 19, 2021
…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
gades pushed a commit to cosanta/cosanta-core that referenced this pull request May 9, 2022
…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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants