Skip to content

Conversation

aureleoules
Copy link
Contributor

@aureleoules aureleoules commented Jul 26, 2022

I added const references to some variables to avoid unnecessarily copying objects.

Also added two clang-tidy checks : performance-for-range-copy and performance-unnecessary-copy-initialization.

@aureleoules aureleoules changed the title 2022 07 cleanup refactor: Cleanup variables Jul 26, 2022
@aureleoules aureleoules changed the title refactor: Cleanup variables refactor: Cleanup unused variables Jul 26, 2022
Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

Concept ACK

  • I agree with removing unused variables, as this makes the code cleaner and free of redundancies.

  • I verified that all the variables in the first and second commit either had their value reassigned before being used or were completely unused in their lifetime.

  • I verified that the variables in the third commit had their values unchanged, so it makes sense to make them a const variable.

  • I think merging the first and second commit would be a good idea as they are similar.

  • I would give the test file another view to confirm no other instance of unused variable has been left in the test file.

@@ -169,7 +169,7 @@ const std::set<BlockFilterType>& AllBlockFilterTypes()

static std::once_flag flag;
std::call_once(flag, []() {
for (auto entry : g_filter_types) {
for (const auto& entry : g_filter_types) {
Copy link
Member

Choose a reason for hiding this comment

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

Those things keep coming up, so it would be good to have a clang-tidy check for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 4407213.

@@ -372,7 +372,6 @@ def test_sequence(self):
# since it greatly depends on inner-workings of blocks/mempool
# during "deep" re-orgs. Probably should "re-construct"
# blockchain/mempool state from notifications instead.
block_count = self.nodes[0].getblockcount()
Copy link
Member

Choose a reason for hiding this comment

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

Dead Python code should be getting caught by Vulture, see https://github.com/bitcoin/bitcoin/blob/master/test/lint/lint-python-dead-code.py. Maybe we need to drop the minimum confidence.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can decrease confidence. But this line worries me a little.

Any value below 100 introduces the risk of false positives, which would create an unacceptable maintenance burden.

Can we afford false positives for the chance of catching the few missed cases?

Copy link
Member

Choose a reason for hiding this comment

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

Can we afford false positives for the chance of catching the few missed cases?

It should either be tweaked to the point that it catches obviously dead code, or just removed. No point running a dead code linter that doesn't catch dead code.

Copy link
Contributor

Choose a reason for hiding this comment

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

No point running a dead code linter that doesn't catch dead code.

I agree with it.

@aureleoules aureleoules force-pushed the 2022-07-cleanup branch 4 times, most recently from 4403522 to 5f99c7a Compare July 26, 2022 16:06
@aureleoules aureleoules changed the title refactor: Cleanup unused variables refactor: Cleanup unused variables and enable two clang-tidy checks Jul 26, 2022
@aureleoules
Copy link
Contributor Author

I've removed more unused variables and added more const references.
Also enabled two clang-tidy checks to enforce const references where applicable.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 26, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25872 (Fix issues when calling std::move(const&) by MarcoFalke)
  • #25695 (tidy: add modernize-use-using by fanquake)
  • #25673 (refactor: make member functions const when applicable by aureleoules)
  • #25429 (refactor: Avoid UniValue copies by MarcoFalke)
  • #24914 (wallet: Load database records in a particular order by achow101)
  • #23417 (wallet, spkm: Move key management from DescriptorScriptPubKeyMan to wallet level KeyManager by achow101)
  • #22341 (rpc: add getxpub by Sjors)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake
Copy link
Member

I've removed more unused variables and added more const references.
Also enabled two clang-tidy checks to enforce const references where applicable.

In a9c0555. Don't mix refactoring Python test code & c++ networking code into the same commit. I'd actually suggest splitting the Python changes from this PR, given it's unclear if Vulture is working. That should be followed up with, and without it, it's not clear if your Python changes are exhaustive.

You should order your commits so that the changes follow logically. i.e perform refactors that allow turning on clang-tidy checks, before turning on the checks, not the other way around.

Please write commit messages that are meaningful, and explain your changes. Limit your commit titles to ~ 50 chars. (i.e 4407213).

This avoids initializing variables with the copy-constructor of a
non-trivially copyable type.
@aureleoules aureleoules changed the title refactor: Cleanup unused variables and enable two clang-tidy checks refactor: Make const references to avoid unnecessarily copying objects and enable two clang-tidy checks Jul 27, 2022
@aureleoules
Copy link
Contributor Author

Thank you for the reviews.

I have addressed your comments @fanquake :

  • dropped commit with python refactor
  • re-ordered and renamed commits

Though, I haven't been able to tweak vulture to detect the unused variables I found manually.

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 46c4dda

Checks enabled: 'performance-for-range-copy' and 'performance-unnecessary-copy-initialization'
Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK ae7ae36

@@ -476,7 +476,7 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
if (recvr != IntrRecvError::OK) {
return error("Error reading from proxy");
}
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
if (InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock) != IntrRecvError::OK) {
return error("Error reading from proxy");
}
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
Copy link
Member

Choose a reason for hiding this comment

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

for reference, the changes in this file are not detected by clang-tidy, nor do they have anything to do with const, but they look correct

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

review ACK ae7ae36

@maflcko maflcko merged commit 9eaef10 into bitcoin:master Aug 19, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 19, 2022
@aureleoules aureleoules deleted the 2022-07-cleanup branch August 25, 2022 10:23
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Oct 18, 2022
…sarily copying objects and enable two clang-tidy checks

ae7ae36 tidy: Enable two clang-tidy checks (Aurèle Oulès)
081b0e5 refactor: Make const refs vars where applicable (Aurèle Oulès)

Pull request description:

  I added const references to some variables to avoid unnecessarily copying objects.

  Also added two clang-tidy checks : [performance-for-range-copy](https://releases.llvm.org/11.1.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-for-range-copy.html) and [performance-unnecessary-copy-initialization](https://releases.llvm.org/12.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/performance-unnecessary-copy-initialization.html).

ACKs for top commit:
  vasild:
    ACK ae7ae36
  MarcoFalke:
    review ACK ae7ae36

Tree-SHA512: f6ac6b0cd0eee1e0c34d2f186484bc0f7ec6071451cccb33fa88a67d93d92b304e2fac378b88f087e94657745bca4e966dbc443759587400eb01b1f3061fde8c
@bitcoin bitcoin locked and limited conversation to collaborators Aug 25, 2023
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.

6 participants