clang-tidy: Add more performance-*
checks and related fixes
#26642
pull
hebasto
wants to merge
4
commits into
bitcoin:master
from
hebasto:221205-ci-tidy
changing
25
files
+37 −14
-
hebasto commented at 6:40 pm on December 5, 2022: member
-
DrahtBot commented at 6:40 pm on December 5, 2022: contributor
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviews
See the guideline for information on the review process.
Type Reviewers ACK martinus, TheCharlatan Stale ACK aureleoules If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.
Conflicts
Reviewers, this pull request conflicts with the following ones:
- #26485 (RPC: Accept options as named-only parameters by ryanofsky)
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.
-
aureleoules commented at 7:39 pm on December 5, 2022: memberConcept ACK
-
in src/.clang-tidy:12 in 66b5bdd791 outdated
20-performance-for-range-copy, 21-performance-move-const-arg, 22-performance-unnecessary-copy-initialization, 23+performance-*, 24+-performance-no-int-to-ptr, 25+-performance-unnecessary-value-param,
aureleoules commented at 10:32 pm on December 5, 2022:Is there a reason why these two are not enabled?
hebasto commented at 10:36 pm on December 5, 2022:Is there a reason why they these two are not enabled?
The required fixes seem too invasive for this PR.
aureleoules approvedaureleoules commented at 10:33 pm on December 5, 2022: memberACK 66b5bdd791aca024996db182cfdec4439895bec0in src/.clang-tidy:10 in 6147104769 outdated
6@@ -7,6 +7,7 @@ modernize-use-default-member-init, 7 modernize-use-nullptr, 8 performance-faster-string-find, 9 performance-for-range-copy, 10+performance-inefficient-string-concatenation,
maflcko commented at 3:24 pm on December 7, 2022:Not sure. This makes the code pretty verbose and it is unclear whether append or strprintf should be used. Also, while this check is disabled for non-loops. In our code it is only triggered on errors in loops.
hebasto force-pushed on Dec 7, 2022hebasto commented at 5:09 pm on December 7, 2022: memberUpdated 66b5bdd791aca024996db182cfdec4439895bec0 -> 0524c30fbb05686a8cf8949a4b05d4a3c9a4c118 (pr26642.01 -> pr26642.02):
- addressed @MarcoFalke comment
hebasto force-pushed on Dec 7, 2022in src/.clang-tidy:12 in 61d7f1e451 outdated
8@@ -9,6 +9,7 @@ performance-faster-string-find, 9 performance-for-range-copy, 10 performance-inefficient-vector-operation, 11 performance-move-const-arg, 12+performance-no-automatic-move,
maflcko commented at 5:53 pm on December 21, 2022:Maybe split this out to make review/testing easier. It may be related to #25429 and https://github.com/bitcoin/bitcoin/pull/26308
maflcko referenced this in commit 9887fc7898 on Jan 11, 2023sidhujag referenced this in commit 3b0597af60 on Jan 11, 2023DrahtBot added the label Needs rebase on Jan 11, 2023hebasto force-pushed on Jan 12, 2023hebasto commented at 3:18 pm on January 12, 2023: memberRebased ecb0262a5a45f545a18c93e42317e013b8740fba -> 1f1b3fdc1872ae30127de363fc8c72f1649626de (pr26642.03 -> pr26642.04) on top of the merged #26758.DrahtBot removed the label Needs rebase on Jan 12, 2023maflcko commented at 12:28 pm on January 17, 2023: memberCI fails. Also, the first commit could be split up?hebasto force-pushed on Jan 17, 2023hebasto commented at 1:49 pm on January 17, 2023: memberRebased 1f1b3fdc1872ae30127de363fc8c72f1649626de -> 38f8938868f8ebebf3353e137957358399704a4b (pr26642.04 -> pr26642.05) on top of the merged bitcoin-core/gui#686.
CI fails.
Should be green now :)
maflcko referenced this in commit f41252f19d on Jan 17, 2023hebasto force-pushed on Jan 17, 2023hebasto commented at 3:55 pm on January 17, 2023: memberRebased 38f8938868f8ebebf3353e137957358399704a4b -> 9e81656e92c91d128db8a5fcaecb0c385bb012cb (pr26642.05 -> pr26642.06) on top of the merged #26905.sidhujag referenced this in commit 8d0c3a0140 on Jan 17, 2023DrahtBot added the label Needs rebase on Feb 1, 2023hebasto force-pushed on Feb 1, 2023hebasto commented at 6:59 pm on February 1, 2023: memberRebased 9e81656e92c91d128db8a5fcaecb0c385bb012cb -> 3f4e43caefb53ea726af07fb0802512264f82ae3 (pr26642.06 -> pr26642.07) on top of the merged #26705 and #26935. @martinus
Mind reviewing changes in
bench/nanobench.h
?DrahtBot removed the label Needs rebase on Feb 1, 2023martinus commented at 7:32 am on February 2, 2023: contributorMind reviewing changes in bench/nanobench.h?
ACK the nanobench.h changes. I’d like to keep the version in sync with the upstream repository at https://github.com/martinus/nanobench
So I’ll have a look at the clang-tidy checks there, do a bunch of updates, and then create a PR to update the nanobench version here to stay in sync. That might take a while though.
hebasto marked this as a draft on Feb 3, 2023fanquake referenced this in commit d8f9826037 on Feb 5, 2023DrahtBot added the label Needs rebase on Feb 5, 2023sidhujag referenced this in commit 965f71901e on Feb 5, 2023hebasto force-pushed on Feb 6, 2023hebasto marked this as ready for review on Feb 6, 2023DrahtBot removed the label Needs rebase on Feb 6, 2023in src/qt/walletmodel.h:126 in 79bf399e59 outdated
122@@ -123,8 +123,8 @@ class WalletModel : public QObject 123 // Copy constructor is disabled. 124 UnlockContext(const UnlockContext&) = delete; 125 // Move operator and constructor transfer the context 126- UnlockContext(UnlockContext&& obj) { CopyFrom(std::move(obj)); } 127- UnlockContext& operator=(UnlockContext&& rhs) { CopyFrom(std::move(rhs)); return *this; } 128+ UnlockContext(UnlockContext&& obj) noexcept { CopyFrom(std::move(obj)); }
martinus commented at 12:36 pm on February 6, 2023:I’m not sure this is correct.CopyFrom
is not noexcept, so marking this as noexcept can be dangerous. CopyFrom seems to use the copy assignment operator with its*this = rhs;
which too is not noexcept. I think it can currently be marked as noexcept (based on the members here), but this is dangerous; e.g. when you add any data that needs allocating, thenoexcept
would be a lie and bad_alloc exceptions would cause the program to abort
hebasto commented at 8:58 am on February 16, 2023:The commented code is no longer relevant since https://github.com/bitcoin-core/gui/pull/711.
Nevertheless, I’ve dropped
performance-noexcept-move-constructor
commit for a follow up as it requires more thorough reviewing for similar cases.
martinus commented at 6:00 pm on February 16, 2023:Good call, I find thenoexcept
stuff so brittle it’s really hard to use. E.g. my changes to nanobench.h too were not completely correct because there’s not even a guarantee thatstd::string
move assignment is noexcept, see here: https://github.com/martinus/nanobench/pull/87hebasto commented at 6:01 pm on February 14, 2023: memberDrafted. Please review https://github.com/bitcoin-core/gui/pull/711 first.hebasto marked this as a draft on Feb 14, 2023DrahtBot added the label Needs rebase on Feb 16, 2023hebasto marked this as ready for review on Feb 16, 2023hebasto force-pushed on Feb 16, 2023DrahtBot removed the label Needs rebase on Feb 16, 2023hebasto commented at 8:59 am on February 16, 2023: memberReady for a (final?) review now :)in src/test/scheduler_tests.cpp:74 in 2cdb84ca4c outdated
70@@ -71,6 +71,7 @@ BOOST_AUTO_TEST_CASE(manythreads) 71 72 // As soon as these are created they will start running and servicing the queue 73 std::vector<std::thread> microThreads; 74+ microThreads.reserve(5);
martinus commented at 6:36 pm on February 16, 2023:nit: could be 10, 5 more are added :)
in src/test/script_p2sh_tests.cpp:279 in 2cdb84ca4c outdated
272 for (int i = 0; i < 6; i++) 273 { 274 key[i].MakeNewKey(true); 275 BOOST_CHECK(keystore.AddKey(key[i])); 276 } 277+ std::vector<CPubKey> keys;
martinus commented at 6:43 pm on February 16, 2023:InBOOST_AUTO_TEST_CASE(set)
there’s anotherstd::vector<CPubKey> keys;
without reserve, I wonder why clang-tidy doesn’t see these
hebasto commented at 2:58 pm on February 22, 2023:I wonder why clang-tidy doesn’t see these
So do I :)
martinus commented at 7:03 pm on February 16, 2023: contributorI ran clang-tidy on this PR, and found a few more warnings:
0/home/martinus/git/github.com/martinus/bitcoin/src/rpc/server.cpp:88:9: error: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 1 vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front())); 2 ^ 3 4/home/martinus/git/github.com/martinus/bitcoin/src/rpc/server.cpp:516:39: error: 'emplace_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 5 for (const auto& i : mapCommands) commandList.emplace_back(i.first); 6 ^ 7 8/home/martinus/git/github.com/martinus/bitcoin/src/rpc/util.cpp:612:9: error: 'emplace_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 9 ret.emplace_back(arg.m_names);
hebasto force-pushed on Feb 22, 2023hebasto commented at 2:57 pm on February 22, 2023: memberUpdated 2cdb84ca4c96cef03946b14ec46a7720f3ece098 -> 7db868b49b60de371d01cda1f8e5faaa0e18f822 (pr26642.09 -> pr26642.10):
- rebased
- addressed @martinus’s #26642 (review)
I ran clang-tidy on this PR, and found a few more warnings
Is there a way to reproduce them in the CI environment?
martinus commented at 3:12 pm on February 22, 2023: contributorI don’t know much about how the CI works, maybe it’s possible to install a newer clang version? I’m using 15.0.7hebasto commented at 3:28 pm on February 22, 2023: memberI don’t know much about how the CI works, maybe it’s possible to install a newer clang version? I’m using 15.0.7
Ah, that makes sense.
Maybe keep this PR diff CI-driven? (I mean, the CI task passes now)
I assume that bumping tools versions in a CI task will force us to fix more warnings, not just those you have already mentioned.
fanquake commented at 9:54 am on February 23, 2023: memberMaybe keep this PR diff CI-driven? (I mean, the CI task passes now)
If there are additional “fixes” being pointed out, which are correct, and based on the same rules that we are trying to apply, I don’t really see the point of leaving those changes out, just to have to fix them later (upgrading our tooling here is another point).
I’ve been a little bit concerned that a lot of these clang-tidy changes have just been blindly following the tool/CI outout, without much thought as to wether changes are actually good/bad/complete or not, and are just being done for the sake of enabling things.
TheCharlatan approvedTheCharlatan commented at 10:34 am on March 6, 2023: contributorACK 7db868b49b60de371d01cda1f8e5faaa0e18f822
I reproduced all changes here by running
clang-tidy
locally. They all seem like a clear improvement to me.DrahtBot requested review from aureleoules on Mar 23, 2023hebasto marked this as a draft on Mar 23, 2023fanquake referenced this in commit f380bb93e8 on Mar 23, 2023hebasto force-pushed on Mar 23, 2023hebasto marked this as ready for review on Mar 23, 2023hebasto marked this as a draft on Mar 23, 2023fanquake commented at 3:27 pm on March 23, 2023: memberSeeing lots of https://cirrus-ci.com/task/6739422046060544?logs=ci#L339:
0/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/rpc/server.cpp:88:9: error: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 1 vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front())); 2 ^ 3/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/rpc/server.cpp:516:39: error: 'emplace_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-i617 warnings generated.
hebasto force-pushed on Mar 23, 2023hebasto marked this as ready for review on Mar 23, 2023hebasto commented at 3:44 pm on March 23, 2023: memberSeeing lots of https://cirrus-ci.com/task/6739422046060544?logs=ci#L339:
Sorry. Pushed a wrong branch.
Should be OK now.
TheCharlatan commented at 4:49 pm on March 23, 2023: contributorre-ACK 09383b6
Locally, I’m also getting a bunch of (though still on clang-14):
0error: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy,-warnings-as-errors]
after running:
0bear --config src/.bear-tidy-config -- make -j $(nproc) 1cd ./src/ && run-clang-tidy -j $(nproc)
in src/wallet/scriptpubkeyman.cpp:2505 in 09383b6122 outdated
2501@@ -2501,6 +2502,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& 2502 } else { 2503 // Maybe there are pubkeys listed that we can sign for 2504 std::vector<CPubKey> pubkeys; 2505+ pubkeys.reserve(input.hd_keypaths.size());
martinus commented at 4:50 pm on March 26, 2023:nit: maybe add+ 2
to the reserve because when the nextif
is true 2 more will be added. Otherwise the nextemplace_back
will most likely cause a capacity change
martinus commented at 4:53 pm on March 26, 2023: contributorcode review ACK 09383b612216b63f2aec0f1fffc889989c66f212clang-tidy: Add `performance-faster-string-find` check
https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-find.html
clang-tidy: Add `performance-inefficient-vector-operation` check
https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html
clang-tidy: Add `performance-type-promotion-in-math-fn` check
https://clang.llvm.org/extra/clang-tidy/checks/performance/type-promotion-in-math-fn.html
clang-tidy: Exclude `performance-*` checks rather including them 03ec5b6f9chebasto force-pushed on Mar 26, 2023hebasto commented at 7:22 pm on March 26, 2023: memberUpdated 09383b612216b63f2aec0f1fffc889989c66f212 -> 03ec5b6f9ca3af28c9ce25cf2393e28ae852d808 (pr26642.12 -> pr26642.13, diff):
martinus commented at 5:46 am on March 27, 2023: contributorACK 03ec5b6f9ca3af28c9ce25cf2393e28ae852d808DrahtBot requested review from TheCharlatan on Mar 27, 2023TheCharlatan commented at 6:08 am on March 27, 2023: contributorre-ACK 03ec5b6DrahtBot removed review request from TheCharlatan on Mar 27, 2023fanquake merged this on Mar 27, 2023fanquake closed this on Mar 27, 2023
hebasto deleted the branch on Mar 27, 2023sidhujag referenced this in commit d060ef7938 on Mar 27, 2023bitcoin locked this on Mar 26, 2024
github-metadata-mirror
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 12:12 UTC
This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me