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
  1. hebasto commented at 6:40 pm on December 5, 2022: member
  2. 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.

  3. aureleoules commented at 7:39 pm on December 5, 2022: member
    Concept ACK
  4. 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.

  5. aureleoules approved
  6. aureleoules commented at 10:33 pm on December 5, 2022: member
    ACK 66b5bdd791aca024996db182cfdec4439895bec0
  7. in 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 commented at 5:10 pm on December 7, 2022:
    Thanks! Updated.
  8. hebasto force-pushed on Dec 7, 2022
  9. hebasto commented at 5:09 pm on December 7, 2022: member

    Updated 66b5bdd791aca024996db182cfdec4439895bec0 -> 0524c30fbb05686a8cf8949a4b05d4a3c9a4c118 (pr26642.01 -> pr26642.02):

  10. hebasto force-pushed on Dec 7, 2022
  11. in 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

    hebasto commented at 3:48 pm on December 27, 2022:

    Maybe split this out to make review/testing easier. It may be related to #25429 and #26308

    Done in #26758.

  12. maflcko referenced this in commit 9887fc7898 on Jan 11, 2023
  13. sidhujag referenced this in commit 3b0597af60 on Jan 11, 2023
  14. DrahtBot added the label Needs rebase on Jan 11, 2023
  15. hebasto force-pushed on Jan 12, 2023
  16. hebasto commented at 3:18 pm on January 12, 2023: member
    Rebased ecb0262a5a45f545a18c93e42317e013b8740fba -> 1f1b3fdc1872ae30127de363fc8c72f1649626de (pr26642.03 -> pr26642.04) on top of the merged #26758.
  17. DrahtBot removed the label Needs rebase on Jan 12, 2023
  18. maflcko commented at 12:28 pm on January 17, 2023: member
    CI fails. Also, the first commit could be split up?
  19. hebasto force-pushed on Jan 17, 2023
  20. hebasto commented at 1:43 pm on January 17, 2023: member

    Also, the first commit could be split up?

    Done in #26905.

  21. hebasto commented at 1:49 pm on January 17, 2023: member

    Rebased 1f1b3fdc1872ae30127de363fc8c72f1649626de -> 38f8938868f8ebebf3353e137957358399704a4b (pr26642.04 -> pr26642.05) on top of the merged bitcoin-core/gui#686.

    CI fails.

    Should be green now :)

  22. maflcko referenced this in commit f41252f19d on Jan 17, 2023
  23. hebasto force-pushed on Jan 17, 2023
  24. hebasto commented at 3:55 pm on January 17, 2023: member
    Rebased 38f8938868f8ebebf3353e137957358399704a4b -> 9e81656e92c91d128db8a5fcaecb0c385bb012cb (pr26642.05 -> pr26642.06) on top of the merged #26905.
  25. sidhujag referenced this in commit 8d0c3a0140 on Jan 17, 2023
  26. DrahtBot added the label Needs rebase on Feb 1, 2023
  27. hebasto force-pushed on Feb 1, 2023
  28. hebasto commented at 6:59 pm on February 1, 2023: member

    Rebased 9e81656e92c91d128db8a5fcaecb0c385bb012cb -> 3f4e43caefb53ea726af07fb0802512264f82ae3 (pr26642.06 -> pr26642.07) on top of the merged #26705 and #26935. @martinus

    Mind reviewing changes in bench/nanobench.h?

  29. DrahtBot removed the label Needs rebase on Feb 1, 2023
  30. martinus commented at 7:32 am on February 2, 2023: contributor

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

  31. martinus commented at 6:16 am on February 3, 2023: contributor
    I created #27030 which compiles without warnings with the .clang-tidy changes here
  32. hebasto commented at 8:54 am on February 3, 2023: member

    I created #27030 which compiles without warnings with the .clang-tidy changes here

    Converting this PR to a draft for now.

  33. hebasto marked this as a draft on Feb 3, 2023
  34. fanquake referenced this in commit d8f9826037 on Feb 5, 2023
  35. DrahtBot added the label Needs rebase on Feb 5, 2023
  36. sidhujag referenced this in commit 965f71901e on Feb 5, 2023
  37. hebasto force-pushed on Feb 6, 2023
  38. hebasto marked this as ready for review on Feb 6, 2023
  39. hebasto commented at 10:59 am on February 6, 2023: member

    I created #27030 which compiles without warnings with the .clang-tidy changes here

    Converting this PR to a draft for now.

    Rebased on the merged #27030, and is ready for a (final?) review :)

  40. DrahtBot removed the label Needs rebase on Feb 6, 2023
  41. in 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, the noexcept would be a lie and bad_alloc exceptions would cause the program to abort

    hebasto commented at 8:58 am on February 16, 2023:

    @martinus

    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 the noexcept 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 that std::string move assignment is noexcept, see here: https://github.com/martinus/nanobench/pull/87
  42. hebasto commented at 6:01 pm on February 14, 2023: member
    Drafted. Please review https://github.com/bitcoin-core/gui/pull/711 first.
  43. hebasto marked this as a draft on Feb 14, 2023
  44. DrahtBot added the label Needs rebase on Feb 16, 2023
  45. hebasto marked this as ready for review on Feb 16, 2023
  46. hebasto force-pushed on Feb 16, 2023
  47. DrahtBot removed the label Needs rebase on Feb 16, 2023
  48. hebasto commented at 8:59 am on February 16, 2023: member
    Ready for a (final?) review now :)
  49. 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 :)

    hebasto commented at 2:58 pm on February 22, 2023:
    Thanks! Fixed.
  50. 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:
    In BOOST_AUTO_TEST_CASE(set) there’s another std::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 :)


    hebasto commented at 3:17 pm on March 23, 2023:
  51. martinus commented at 7:03 pm on February 16, 2023: contributor

    I 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);
    
  52. hebasto force-pushed on Feb 22, 2023
  53. hebasto commented at 2:57 pm on February 22, 2023: member

    Updated 2cdb84ca4c96cef03946b14ec46a7720f3ece098 -> 7db868b49b60de371d01cda1f8e5faaa0e18f822 (pr26642.09 -> pr26642.10):


    @martinus wrote:

    I ran clang-tidy on this PR, and found a few more warnings

    Is there a way to reproduce them in the CI environment?

  54. martinus commented at 3:12 pm on February 22, 2023: contributor
    I 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
  55. hebasto commented at 3:28 pm on February 22, 2023: member

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

  56. fanquake commented at 9:54 am on February 23, 2023: member

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

  57. TheCharlatan approved
  58. TheCharlatan commented at 10:34 am on March 6, 2023: contributor

    ACK 7db868b49b60de371d01cda1f8e5faaa0e18f822

    I reproduced all changes here by running clang-tidy locally. They all seem like a clear improvement to me.

  59. hebasto commented at 9:08 am on March 23, 2023: member

    I 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

    Done in #27311.

    Making this PR a draft for now.

  60. DrahtBot requested review from aureleoules on Mar 23, 2023
  61. hebasto marked this as a draft on Mar 23, 2023
  62. fanquake referenced this in commit f380bb93e8 on Mar 23, 2023
  63. hebasto force-pushed on Mar 23, 2023
  64. hebasto marked this as ready for review on Mar 23, 2023
  65. hebasto commented at 3:17 pm on March 23, 2023: member

    Rebased on top of the merged #27311.

    All @martinus’s comments have been addressed.

  66. hebasto marked this as a draft on Mar 23, 2023
  67. fanquake commented at 3:27 pm on March 23, 2023: member

    Seeing 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.
    
  68. hebasto force-pushed on Mar 23, 2023
  69. hebasto marked this as ready for review on Mar 23, 2023
  70. hebasto commented at 3:44 pm on March 23, 2023: member

    Seeing lots of https://cirrus-ci.com/task/6739422046060544?logs=ci#L339:

    Sorry. Pushed a wrong branch.

    Should be OK now.

  71. TheCharlatan commented at 4:49 pm on March 23, 2023: contributor

    re-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)
    
  72. 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 next if is true 2 more will be added. Otherwise the next emplace_back will most likely cause a capacity change

    hebasto commented at 7:22 pm on March 26, 2023:
    Thanks! Updated.
  73. martinus commented at 4:53 pm on March 26, 2023: contributor
    code review ACK 09383b612216b63f2aec0f1fffc889989c66f212
  74. clang-tidy: Add `performance-faster-string-find` check
    https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-find.html
    516b75f66e
  75. clang-tidy: Add `performance-inefficient-vector-operation` check
    https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html
    7e975e6cf8
  76. 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
    2400437230
  77. clang-tidy: Exclude `performance-*` checks rather including them 03ec5b6f9c
  78. hebasto force-pushed on Mar 26, 2023
  79. hebasto commented at 7:22 pm on March 26, 2023: member

    Updated 09383b612216b63f2aec0f1fffc889989c66f212 -> 03ec5b6f9ca3af28c9ce25cf2393e28ae852d808 (pr26642.12 -> pr26642.13, diff):

  80. martinus commented at 5:46 am on March 27, 2023: contributor
    ACK 03ec5b6f9ca3af28c9ce25cf2393e28ae852d808
  81. DrahtBot requested review from TheCharlatan on Mar 27, 2023
  82. TheCharlatan commented at 6:08 am on March 27, 2023: contributor
    re-ACK 03ec5b6
  83. DrahtBot removed review request from TheCharlatan on Mar 27, 2023
  84. fanquake merged this on Mar 27, 2023
  85. fanquake closed this on Mar 27, 2023

  86. hebasto deleted the branch on Mar 27, 2023
  87. sidhujag referenced this in commit d060ef7938 on Mar 27, 2023
  88. bitcoin 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