Skip to content

Commit 7e975e6

Browse files
committed
clang-tidy: Add performance-inefficient-vector-operation check
https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html
1 parent 516b75f commit 7e975e6

22 files changed

+27
-4
lines changed

src/.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ modernize-use-default-member-init,
77
modernize-use-nullptr,
88
performance-faster-string-find,
99
performance-for-range-copy,
10+
performance-inefficient-vector-operation,
1011
performance-move-const-arg,
1112
performance-no-automatic-move,
1213
performance-unnecessary-copy-initialization,

src/bench/lockedpool.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ static void BenchLockedPool(benchmark::Bench& bench)
1717
const size_t synth_size = 1024*1024;
1818
Arena b(synth_base, synth_size, 16);
1919

20-
std::vector<void*> addr;
21-
for (int x=0; x<ASIZE; ++x)
22-
addr.push_back(nullptr);
20+
std::vector<void*> addr{ASIZE, nullptr};
2321
uint32_t s = 0x12345678;
2422
bench.run([&] {
2523
int idx = s & (addr.size() - 1);

src/bench/wallet_create_tx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType
146146

147147
// Generate destinations
148148
std::vector<CScript> dest_wallet;
149+
dest_wallet.reserve(output_type.size());
149150
for (auto type : output_type) {
150151
dest_wallet.emplace_back(GetScriptForDestination(getNewDestination(wallet, type)));
151152
}

src/bitcoin-cli.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ static void ParseGetInfoResult(UniValue& result)
10321032
}
10331033

10341034
std::vector<std::string> formatted_proxies;
1035+
formatted_proxies.reserve(ordered_proxies.size());
10351036
for (const std::string& proxy : ordered_proxies) {
10361037
formatted_proxies.emplace_back(strprintf("%s (%s)", proxy, Join(proxy_networks.find(proxy)->second, ", ")));
10371038
}

src/bitcoin-util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
127127

128128
std::vector<std::thread> threads;
129129
int n_tasks = std::max(1u, std::thread::hardware_concurrency());
130+
threads.reserve(n_tasks);
130131
for (int i = 0; i < n_tasks; ++i) {
131132
threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
132133
}

src/node/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class NodeImpl : public Node
241241
if (command == "") return {};
242242
ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
243243
std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
244+
result.reserve(signers.size());
244245
for (auto& signer : signers) {
245246
result.emplace_back(std::make_unique<ExternalSignerImpl>(std::move(signer)));
246247
}

src/rpc/server.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
8383
std::string category;
8484
std::set<intptr_t> setDone;
8585
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
86+
vCommands.reserve(mapCommands.size());
8687

8788
for (const auto& entry : mapCommands)
8889
vCommands.push_back(make_pair(entry.second.front()->category + entry.first, entry.second.front()));
@@ -513,6 +514,7 @@ static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& req
513514
std::vector<std::string> CRPCTable::listCommands() const
514515
{
515516
std::vector<std::string> commandList;
517+
commandList.reserve(mapCommands.size());
516518
for (const auto& i : mapCommands) commandList.emplace_back(i.first);
517519
return commandList;
518520
}

src/rpc/util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
608608
std::vector<std::string> RPCHelpMan::GetArgNames() const
609609
{
610610
std::vector<std::string> ret;
611+
ret.reserve(m_args.size());
611612
for (const auto& arg : m_args) {
612613
ret.emplace_back(arg.m_names);
613614
}

src/script/descriptor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ class MultiADescriptor final : public DescriptorImpl
830830
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override {
831831
CScript ret;
832832
std::vector<XOnlyPubKey> xkeys;
833+
xkeys.reserve(keys.size());
833834
for (const auto& key : keys) xkeys.emplace_back(key);
834835
if (m_sorted) std::sort(xkeys.begin(), xkeys.end());
835836
ret << ToByteVector(xkeys[0]) << OP_CHECKSIG;

src/test/allocator_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ BOOST_AUTO_TEST_CASE(arena_tests)
7777
b.walk();
7878
#endif
7979
// Sweeping allocate all memory
80+
addr.reserve(2048);
8081
for (int x=0; x<1024; ++x)
8182
addr.push_back(b.alloc(1024));
8283
BOOST_CHECK(b.stats().free == 0);

0 commit comments

Comments
 (0)