Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin/bitcoin#31305: refactor: Fix remaining clang-tidy perfo…
…rmance-inefficient-vector errors 11f3bc2 refactor: Reserve vectors in fuzz tests (Lőrinc) 152fefe refactor: Preallocate PrevectorFillVector(In)Direct without vector resize (Lőrinc) a774c7a refactor: Fix remaining clang-tidy performance-inefficient-vector errors (Lőrinc) Pull request description: PR inspired by bitcoin/bitcoin#29608 (comment) (and bitcoin/bitcoin#29458, bitcoin/bitcoin#29606, bitcoin/bitcoin#29607, bitcoin/bitcoin#30093). The `clang-tidy` check can be run via: ```bash cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_FOR_FUZZING=ON && cmake --build build -j$(nproc) run-clang-tidy -quiet -p build -j $(nproc) -checks='-*,performance-inefficient-vector-operation' | grep -v 'clang-tidy' ``` which revealed 3 tests and 1 prod warning (+ fuzz and benching, found by hebasto). Even though the tests aren't performance critical, getting rid of these warnings (for which the checks were already enabled via https://github.com/bitcoin/bitcoin/blob/master/src/.clang-tidy#L18, see below), the fix was quite simple. <details> <summary>clang-tidy -list-checks</summary> ```bash cd src && clang-tidy -list-checks | grep 'vector' performance-inefficient-vector-operation ``` </details> <details> <summary>Output before the change</summary> ``` src/test/rpc_tests.cpp:434: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] 433 | for (int64_t i = 0; i < 100; i++) { 434 | feerates.emplace_back(1 ,1); | ^ src/test/checkqueue_tests.cpp:366:13: error: 'emplace_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 365 | for (size_t i = 0; i < 3; ++i) { 366 | tg.emplace_back( | ^ src/test/cuckoocache_tests.cpp:231: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] 228 | for (uint32_t x = 0; x < 3; ++x) 229 | /** Each thread is emplaced with x copy-by-value 230 | */ 231 | threads.emplace_back([&, x] { | ^ src/rpc/output_script.cpp:127:17: error: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation,-warnings-as-errors] 126 | for (unsigned int i = 0; i < keys.size(); ++i) { 127 | pubkeys.push_back(HexToPubKey(keys[i].get_str())); | ^ ``` And the fuzz and benchmarks, noticed by hebasto: bitcoin/bitcoin#31305 (comment) </details> ACKs for top commit: maflcko: review ACK 11f3bc2 🎦 achow101: ACK 11f3bc2 theuni: ACK 11f3bc2 hebasto: ACK 11f3bc2, tested with clang 19.1.5 + clang-tidy. Tree-SHA512: 41691c19f35c63b922a95407617a54f9bff1af3f95f99d15642064f321df038aeb1ae5f061f854ed913f69036807cc28fa6222b2ff4c24ef43b909027fa0f9b3
- Loading branch information