aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2023-03-26 20:17:55 +0100
committerHennadii Stepanov <32963518+hebasto@users.noreply.github.com>2023-03-26 20:17:55 +0100
commit7e975e6cf86617346c1d8e2568f74a0252c03857 (patch)
tree4c102e4b6a5139106aa9b2f085ab57a68a36953a /src
parent516b75f66ec3ba7495fc028c750937bd66cc9bba (diff)
clang-tidy: Add `performance-inefficient-vector-operation` check
https://clang.llvm.org/extra/clang-tidy/checks/performance/inefficient-vector-operation.html
Diffstat (limited to 'src')
-rw-r--r--src/.clang-tidy1
-rw-r--r--src/bench/lockedpool.cpp4
-rw-r--r--src/bench/wallet_create_tx.cpp1
-rw-r--r--src/bitcoin-cli.cpp1
-rw-r--r--src/bitcoin-util.cpp1
-rw-r--r--src/node/interfaces.cpp1
-rw-r--r--src/rpc/server.cpp2
-rw-r--r--src/rpc/util.cpp1
-rw-r--r--src/script/descriptor.cpp1
-rw-r--r--src/test/allocator_tests.cpp1
-rw-r--r--src/test/fuzz/tx_pool.cpp1
-rw-r--r--src/test/getarg_tests.cpp1
-rw-r--r--src/test/policyestimator_tests.cpp1
-rw-r--r--src/test/scheduler_tests.cpp2
-rw-r--r--src/test/script_p2sh_tests.cpp4
-rw-r--r--src/test/txrequest_tests.cpp1
-rw-r--r--src/test/util/net.cpp1
-rw-r--r--src/test/util_threadnames_tests.cpp1
-rw-r--r--src/test/validation_block_tests.cpp1
-rw-r--r--src/wallet/bdb.cpp1
-rw-r--r--src/wallet/rpc/backup.cpp1
-rw-r--r--src/wallet/scriptpubkeyman.cpp2
22 files changed, 27 insertions, 4 deletions
diff --git a/src/.clang-tidy b/src/.clang-tidy
index dc26a640b6..b5620b3675 100644
--- a/src/.clang-tidy
+++ b/src/.clang-tidy
@@ -7,6 +7,7 @@ modernize-use-default-member-init,
modernize-use-nullptr,
performance-faster-string-find,
performance-for-range-copy,
+performance-inefficient-vector-operation,
performance-move-const-arg,
performance-no-automatic-move,
performance-unnecessary-copy-initialization,
diff --git a/src/bench/lockedpool.cpp b/src/bench/lockedpool.cpp
index 161f9af621..6851ed0bd8 100644
--- a/src/bench/lockedpool.cpp
+++ b/src/bench/lockedpool.cpp
@@ -17,9 +17,7 @@ static void BenchLockedPool(benchmark::Bench& bench)
const size_t synth_size = 1024*1024;
Arena b(synth_base, synth_size, 16);
- std::vector<void*> addr;
- for (int x=0; x<ASIZE; ++x)
- addr.push_back(nullptr);
+ std::vector<void*> addr{ASIZE, nullptr};
uint32_t s = 0x12345678;
bench.run([&] {
int idx = s & (addr.size() - 1);
diff --git a/src/bench/wallet_create_tx.cpp b/src/bench/wallet_create_tx.cpp
index bd32a5abdc..80d23d1e51 100644
--- a/src/bench/wallet_create_tx.cpp
+++ b/src/bench/wallet_create_tx.cpp
@@ -146,6 +146,7 @@ static void AvailableCoins(benchmark::Bench& bench, const std::vector<OutputType
// Generate destinations
std::vector<CScript> dest_wallet;
+ dest_wallet.reserve(output_type.size());
for (auto type : output_type) {
dest_wallet.emplace_back(GetScriptForDestination(getNewDestination(wallet, type)));
}
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp
index eb52b30ae4..94ed765455 100644
--- a/src/bitcoin-cli.cpp
+++ b/src/bitcoin-cli.cpp
@@ -1032,6 +1032,7 @@ static void ParseGetInfoResult(UniValue& result)
}
std::vector<std::string> formatted_proxies;
+ formatted_proxies.reserve(ordered_proxies.size());
for (const std::string& proxy : ordered_proxies) {
formatted_proxies.emplace_back(strprintf("%s (%s)", proxy, Join(proxy_networks.find(proxy)->second, ", ")));
}
diff --git a/src/bitcoin-util.cpp b/src/bitcoin-util.cpp
index cf59ca9bff..c0ba4df2be 100644
--- a/src/bitcoin-util.cpp
+++ b/src/bitcoin-util.cpp
@@ -127,6 +127,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
std::vector<std::thread> threads;
int n_tasks = std::max(1u, std::thread::hardware_concurrency());
+ threads.reserve(n_tasks);
for (int i = 0; i < n_tasks; ++i) {
threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
}
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
index b397661df4..50ed4c2ab8 100644
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -241,6 +241,7 @@ public:
if (command == "") return {};
ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
+ result.reserve(signers.size());
for (auto& signer : signers) {
result.emplace_back(std::make_unique<ExternalSignerImpl>(std::move(signer)));
}
diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp
index 44d7e2676b..392238a1fd 100644
--- a/src/rpc/server.cpp
+++ b/src/rpc/server.cpp
@@ -83,6 +83,7 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
std::string category;
std::set<intptr_t> setDone;
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
+ vCommands.reserve(mapCommands.size());
for (const auto& entry : mapCommands)
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
std::vector<std::string> CRPCTable::listCommands() const
{
std::vector<std::string> commandList;
+ commandList.reserve(mapCommands.size());
for (const auto& i : mapCommands) commandList.emplace_back(i.first);
return commandList;
}
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index ae1440c70b..ee9e3544a4 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -608,6 +608,7 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
std::vector<std::string> RPCHelpMan::GetArgNames() const
{
std::vector<std::string> ret;
+ ret.reserve(m_args.size());
for (const auto& arg : m_args) {
ret.emplace_back(arg.m_names);
}
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp
index 857fee1818..b4ad1e3f10 100644
--- a/src/script/descriptor.cpp
+++ b/src/script/descriptor.cpp
@@ -830,6 +830,7 @@ protected:
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override {
CScript ret;
std::vector<XOnlyPubKey> xkeys;
+ xkeys.reserve(keys.size());
for (const auto& key : keys) xkeys.emplace_back(key);
if (m_sorted) std::sort(xkeys.begin(), xkeys.end());
ret << ToByteVector(xkeys[0]) << OP_CHECKSIG;
diff --git a/src/test/allocator_tests.cpp b/src/test/allocator_tests.cpp
index 715ce0a5b4..f74e50a890 100644
--- a/src/test/allocator_tests.cpp
+++ b/src/test/allocator_tests.cpp
@@ -77,6 +77,7 @@ BOOST_AUTO_TEST_CASE(arena_tests)
b.walk();
#endif
// Sweeping allocate all memory
+ addr.reserve(2048);
for (int x=0; x<1024; ++x)
addr.push_back(b.alloc(1024));
BOOST_CHECK(b.stats().free == 0);
diff --git a/src/test/fuzz/tx_pool.cpp b/src/test/fuzz/tx_pool.cpp
index 0cabaf323b..a055705575 100644
--- a/src/test/fuzz/tx_pool.cpp
+++ b/src/test/fuzz/tx_pool.cpp
@@ -316,6 +316,7 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
MockTime(fuzzed_data_provider, chainstate);
std::vector<uint256> txids;
+ txids.reserve(g_outpoints_coinbase_init_mature.size());
for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
txids.push_back(outpoint.hash);
}
diff --git a/src/test/getarg_tests.cpp b/src/test/getarg_tests.cpp
index 9c2d8a4dad..9106040b84 100644
--- a/src/test/getarg_tests.cpp
+++ b/src/test/getarg_tests.cpp
@@ -29,6 +29,7 @@ void ResetArgs(ArgsManager& local_args, const std::string& strArg)
// Convert to char*:
std::vector<const char*> vecChar;
+ vecChar.reserve(vecArg.size());
for (const std::string& s : vecArg)
vecChar.push_back(s.c_str());
diff --git a/src/test/policyestimator_tests.cpp b/src/test/policyestimator_tests.cpp
index 75ba9972f6..bc9c672560 100644
--- a/src/test/policyestimator_tests.cpp
+++ b/src/test/policyestimator_tests.cpp
@@ -24,6 +24,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
CAmount basefee(2000);
CAmount deltaFee(100);
std::vector<CAmount> feeV;
+ feeV.reserve(10);
// Populate vectors of increasing fees
for (int j = 0; j < 10; j++) {
diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp
index 1301a1b219..3fb3378598 100644
--- a/src/test/scheduler_tests.cpp
+++ b/src/test/scheduler_tests.cpp
@@ -71,6 +71,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
// As soon as these are created they will start running and servicing the queue
std::vector<std::thread> microThreads;
+ microThreads.reserve(10);
for (int i = 0; i < 5; i++)
microThreads.emplace_back(std::bind(&CScheduler::serviceQueue, &microTasks));
@@ -136,6 +137,7 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
// the extra threads should effectively be doing nothing
// if they don't we'll get out of order behaviour
std::vector<std::thread> threads;
+ threads.reserve(5);
for (int i = 0; i < 5; ++i) {
threads.emplace_back([&] { scheduler.serviceQueue(); });
}
diff --git a/src/test/script_p2sh_tests.cpp b/src/test/script_p2sh_tests.cpp
index c9f002b324..739ab75de3 100644
--- a/src/test/script_p2sh_tests.cpp
+++ b/src/test/script_p2sh_tests.cpp
@@ -156,6 +156,7 @@ BOOST_AUTO_TEST_CASE(set)
FillableSigningProvider keystore;
CKey key[4];
std::vector<CPubKey> keys;
+ keys.reserve(4);
for (int i = 0; i < 4; i++)
{
key[i].MakeNewKey(true);
@@ -270,12 +271,13 @@ BOOST_AUTO_TEST_CASE(AreInputsStandard)
CCoinsViewCache coins(&coinsDummy);
FillableSigningProvider keystore;
CKey key[6];
- std::vector<CPubKey> keys;
for (int i = 0; i < 6; i++)
{
key[i].MakeNewKey(true);
BOOST_CHECK(keystore.AddKey(key[i]));
}
+ std::vector<CPubKey> keys;
+ keys.reserve(3);
for (int i = 0; i < 3; i++)
keys.push_back(key[i].GetPubKey());
diff --git a/src/test/txrequest_tests.cpp b/src/test/txrequest_tests.cpp
index 17a55d5ab5..dc257a0d51 100644
--- a/src/test/txrequest_tests.cpp
+++ b/src/test/txrequest_tests.cpp
@@ -386,6 +386,7 @@ void BuildBigPriorityTest(Scenario& scenario, int peers)
}
// Make a list of all peers, in order of intended request order (concatenation of pref_peers and npref_peers).
std::vector<NodeId> request_order;
+ request_order.reserve(num_pref + num_npref);
for (int i = 0; i < num_pref; ++i) request_order.push_back(pref_peers[i]);
for (int i = 0; i < num_npref; ++i) request_order.push_back(npref_peers[i]);
diff --git a/src/test/util/net.cpp b/src/test/util/net.cpp
index ac5dfe9e73..bdbaf8d4a9 100644
--- a/src/test/util/net.cpp
+++ b/src/test/util/net.cpp
@@ -95,6 +95,7 @@ bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) con
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candidates, FastRandomContext& random_context)
{
std::vector<NodeEvictionCandidate> candidates;
+ candidates.reserve(n_candidates);
for (int id = 0; id < n_candidates; ++id) {
candidates.push_back({
/*id=*/id,
diff --git a/src/test/util_threadnames_tests.cpp b/src/test/util_threadnames_tests.cpp
index dbb5423a77..ae913939e8 100644
--- a/src/test/util_threadnames_tests.cpp
+++ b/src/test/util_threadnames_tests.cpp
@@ -38,6 +38,7 @@ std::set<std::string> RenameEnMasse(int num_threads)
names.insert(util::ThreadGetInternalName());
};
+ threads.reserve(num_threads);
for (int i = 0; i < num_threads; ++i) {
threads.push_back(std::thread(RenameThisThread, i));
}
diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp
index 4c8687ce69..e08f2c98c2 100644
--- a/src/test/validation_block_tests.cpp
+++ b/src/test/validation_block_tests.cpp
@@ -174,6 +174,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
// this will create parallelism and randomness inside validation - the ValidationInterface
// will subscribe to events generated during block validation and assert on ordering invariance
std::vector<std::thread> threads;
+ threads.reserve(10);
for (int i = 0; i < 10; i++) {
threads.emplace_back([&]() {
bool ignored;
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index 653115aa81..4d3285333f 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -432,6 +432,7 @@ void BerkeleyEnvironment::ReloadDbEnv()
});
std::vector<fs::path> filenames;
+ filenames.reserve(m_databases.size());
for (const auto& it : m_databases) {
filenames.push_back(it.first);
}
diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp
index 09cfc07bc2..9b6b3d629c 100644
--- a/src/wallet/rpc/backup.cpp
+++ b/src/wallet/rpc/backup.cpp
@@ -752,6 +752,7 @@ RPCHelpMan dumpwallet()
// sort time/key pairs
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
+ vKeyBirth.reserve(mapKeyBirth.size());
for (const auto& entry : mapKeyBirth) {
vKeyBirth.push_back(std::make_pair(entry.second, entry.first));
}
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index 1589e52deb..a1956049e2 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -1507,6 +1507,7 @@ std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& p
FlatSigningProvider out;
InferDescriptor(spk, provider)->Expand(0, DUMMY_SIGNING_PROVIDER, dummy, out);
std::vector<CKeyID> ret;
+ ret.reserve(out.pubkeys.size());
for (const auto& entry : out.pubkeys) {
ret.push_back(entry.first);
}
@@ -2501,6 +2502,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
} else {
// Maybe there are pubkeys listed that we can sign for
std::vector<CPubKey> pubkeys;
+ pubkeys.reserve(input.hd_keypaths.size() + 2);
// ECDSA Pubkeys
for (const auto& [pk, _] : input.hd_keypaths) {