8000 clang-tidy: Add more `performance-*` checks and related fixes by hebasto · Pull Request #26642 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

clang-tidy: Add more performance-* checks and related fixes #26642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
10000
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ bugprone-use-after-move,
misc-unused-using-decls,
modernize-use-default-member-init,
modernize-use-nullptr,
performance-for-range-copy,
performance-move-const-arg,
performance-no-automatic-move,
performance-unnecessary-copy-initialization,
performance-*,
-performance-inefficient-string-concatenation,
-performance-no-int-to-ptr,
-performance-noexcept-move-constructor,
-performance-unnecessary-value-param,
readability-const-return-type,
readability-redundant-declaration,
readability-redundant-string-init,
Expand Down
4 changes: 1 addition & 3 deletions src/bench/lockedpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/bench/wallet_create_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ")));
}
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
1 change: 1 addition & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class NodeImpl : public Node
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)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/qt/trafficgraphwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *)
painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h);

// decide what order of magnitude we are
int base = floor(log10(fMax));
float val = pow(10.0f, base);
int base = std::floor(std::log10(fMax));
float val = std::pow(10.0f, base);

const QString units = tr("kB/s");
const float yMarginText = 2.0;
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -732,12 +733,12 @@ UniValue RPCArg::MatchesType(const UniValue& request) const

std::string RPCArg::GetFirstName() const
{
return m_names.substr(0, m_names.find("|"));
return m_names.substr(0, m_names.find('|'));
}

std::string RPCArg::GetName() const
{
CHECK_NONFATAL(std::string::npos == m_names.find("|"));
CHECK_NONFATAL(std::string::npos == m_names.find('|'));
return m_names;
}

Expand Down
1 change: 1 addition & 0 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ class MultiADescriptor final : public DescriptorImpl
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;
Expand Down
1 change: 1 addition & 0 deletions src/test/allocator_tests.cpp
10000
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/tx_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/getarg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
1 change: 1 addition & 0 deletions src/test/policyestimator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/scheduler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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(); });
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/script_p2sh_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down
1 change: 1 addition & 0 deletions src/test/txrequest_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
1 change: 1 addition & 0 deletions src/test/util/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/test/util_threadnames_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
1 change: 1 addition & 0 deletions src/test/validation_block_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/util/bip32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypa
}
// Finds whether it is hardened
uint32_t path = 0;
size_t pos = item.find("'");
size_t pos = item.find('\'');
if (pos != std::string::npos) {
// The hardened tick can only be in the last index of the string
if (pos != item.size() - 1) {
Expand Down
1 change: 1 addition & 0 deletions src/wallet/bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/wallet/rpc/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/zmq/zmqpublishnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static bool IsZMQAddressIPV6(const std::string &zmq_address)
{
const std::string tcp_prefix = "tcp://";
const size_t tcp_index = zmq_address.rfind(tcp_prefix);
const size_t colon_index = zmq_address.rfind(":");
const size_t colon_index = zmq_address.rfind(':');
if (tcp_index == 0 && colon_index != std::string::npos) {
const std::string ip = zmq_address.substr(tcp_prefix.length(), colon_index - tcp_prefix.length());
CNetAddr addr;
Expand Down
0