PR inspired by #29608 (comment) (and #29458, #29606, #29607, #30093).
The clang-tidy
check can be run via:
0cmake -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)
1
2run-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.
0cd src && clang-tidy -list-checks | grep 'vector'
1 performance-inefficient-vector-operation
0src/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]
1 433 | for (int64_t i = 0; i < 100; i++) {
2 434 | feerates.emplace_back(1 ,1);
3 | ^
4
5src/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]
6 365 | for (size_t i = 0; i < 3; ++i) {
7 366 | tg.emplace_back(
8 | ^
9
10src/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]
11 228 | for (uint32_t x = 0; x < 3; ++x)
12 229 | /** Each thread is emplaced with x copy-by-value
13 230 | */
14 231 | threads.emplace_back([&, x] {
15 | ^
16
17src/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]
18 126 | for (unsigned int i = 0; i < keys.size(); ++i) {
19 127 | pubkeys.push_back(HexToPubKey(keys[i].get_str()));
20 | ^
And the fuzz and benchmarks, noticed by hebasto: #31305 (comment)