refactor: Avoid copies by using const references or by move-construction
What changed, and why it matters
This commit is a routine code cleanup that avoids unnecessary copying of strings and objects by using references or moving values instead. It does not change program behavior or fix any security bug.
No security action needed. Treat as normal refactoring/performance improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch is a pure C++ performance/refactoring change. It replaces several pass-by-value parameters with const references or move-construction (e.g., ExternalSigner constructor, Proxy string constructor, PSBTInputSignedAndVerified PartiallySignedTransaction parameter, HandleWalletError shared_ptr parameter, GetNewDestination label parameter). No logic, validation, or trust boundary behavior is altered. The changes only reduce object copies.
Changed components
src/external_signer.cpp/hsrc/netbase.hsrc/psbt.cpp/hsrc/test/argsman_tests.cppsrc/wallet/rpc/util.cpp/hsrc/wallet/wallet.cpp/hInspect captured patch +15 / −13
diff --git a/src/external_signer.cpp b/src/external_signer.cpp
index 33129199..84d98a19 100644
--- a/src/external_signer.cpp
+++ b/src/external_signer.cpp
@@ -15,14 +15,15 @@
#include <string>
#include <vector>
-ExternalSigner::ExternalSigner(const std::string& command, const std::string chain, const std::string& fingerprint, const std::string name): m_command(command), m_chain(chain), m_fingerprint(fingerprint), m_name(name) {}
+ExternalSigner::ExternalSigner(std::string command, std::string chain, std::string fingerprint, std::string name)
+ : m_command{std::move(command)}, m_chain{std::move(chain)}, m_fingerprint{std::move(fingerprint)}, m_name{std::move(name)} {}
std::string ExternalSigner::NetworkArg() const
{
return " --chain " + m_chain;
}
-bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string chain)
+bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain)
{
// Call <command> enumerate
const UniValue result = RunCommandParseJSON(command + " enumerate");
diff --git a/src/external_signer.h b/src/external_signer.h
index 318b3bc0..5d23cef4 100644
--- a/src/external_signer.h
+++ b/src/external_signer.h
@@ -31,7 +31,7 @@ public:
//! @param[in] fingerprint master key fingerprint of the signer
//! @param[in] chain "main", "test", "regtest" or "signet"
//! @param[in] name device name
- ExternalSigner(const std::string& command, const std::string chain, const std::string& fingerprint, const std::string name);
+ ExternalSigner(std::string command, std::string chain, std::string fingerprint, std::string name);
//! Master key fingerprint of the signer
std::string m_fingerprint;
@@ -44,7 +44,7 @@ public:
//! @param[in,out] signers vector to which new signers (with a unique master key fingerprint) are added
//! @param chain "main", "test", "regtest" or "signet"
//! @returns success
- static bool Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string chain);
+ static bool Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain);
//! Display address on the device. Calls `<command> displayaddress --desc <descriptor>`.
//! @param[in] descriptor Descriptor specifying which address to display.
diff --git a/src/netbase.h b/src/netbase.h
index d3c263f9..174eae43 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -60,7 +60,8 @@ class Proxy
public:
Proxy() : m_is_unix_socket(false), m_tor_stream_isolation(false) {}
explicit Proxy(const CService& _proxy, bool tor_stream_isolation = false) : proxy(_proxy), m_is_unix_socket(false), m_tor_stream_isolation(tor_stream_isolation) {}
- explicit Proxy(const std::string path, bool tor_stream_isolation = false) : m_unix_socket_path(path), m_is_unix_socket(true), m_tor_stream_isolation(tor_stream_isolation) {}
+ explicit Proxy(std::string path, bool tor_stream_isolation = false)
+ : m_unix_socket_path(std::move(path)), m_is_unix_socket(true), m_tor_stream_isolation(tor_stream_isolation) {}
CService proxy;
std::string m_unix_socket_path;
diff --git a/src/psbt.cpp b/src/psbt.cpp
index 9339bf7c..a1eafbc0 100644
--- a/src/psbt.cpp
+++ b/src/psbt.cpp
@@ -322,7 +322,7 @@ bool PSBTInputSigned(const PSBTInput& input)
return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
}
-bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
+bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
{
CTxOut utxo;
assert(input_index < psbt.inputs.size());
diff --git a/src/psbt.h b/src/psbt.h
index 9d7fc05b..1a426a2e 100644
--- a/src/psbt.h
+++ b/src/psbt.h
@@ -1413,7 +1413,7 @@ PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction&
bool PSBTInputSigned(const PSBTInput& input);
/** Checks whether a PSBTInput is already signed by doing script verification using final fields. */
-bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);
+bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);
/** Signs a PSBTInput, verifying that all provided data matches what is being signed.
*
diff --git a/src/test/argsman_tests.cpp b/src/test/argsman_tests.cpp
index 5236d5db..e91ae5e9 100644
--- a/src/test/argsman_tests.cpp
+++ b/src/test/argsman_tests.cpp
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(util_datadir)
struct TestArgsManager : public ArgsManager
{
TestArgsManager() { m_network_only_args.clear(); }
- void ReadConfigString(const std::string str_config)
+ void ReadConfigString(const std::string& str_config)
{
std::istringstream streamConfig(str_config);
{
@@ -63,7 +63,7 @@ struct TestArgsManager : public ArgsManager
std::string error;
BOOST_REQUIRE(ReadConfigStream(streamConfig, "", error));
}
- void SetNetworkOnlyArg(const std::string arg)
+ void SetNetworkOnlyArg(const std::string& arg)
{
LOCK(cs_args);
m_network_only_args.insert(arg);
diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp
index d68e6c65..a699b226 100644
--- a/src/wallet/rpc/util.cpp
+++ b/src/wallet/rpc/util.cpp
@@ -124,7 +124,7 @@ void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey,
entry.pushKV("parent_descs", std::move(parent_descs));
}
-void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
+void HandleWalletError(const std::shared_ptr<CWallet>& wallet, DatabaseStatus& status, bilingual_str& error)
{
if (!wallet) {
// Map bad format to not found, since bad format is returned when the
diff --git a/src/wallet/rpc/util.h b/src/wallet/rpc/util.h
index d6497214..89729218 100644
--- a/src/wallet/rpc/util.h
+++ b/src/wallet/rpc/util.h
@@ -54,7 +54,7 @@ std::string LabelFromValue(const UniValue& value);
//! Fetch parent descriptors of this scriptPubKey.
void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry);
-void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error);
+void HandleWalletError(const std::shared_ptr<CWallet>& wallet, DatabaseStatus& status, bilingual_str& error);
void AppendLastProcessedBlock(UniValue& entry, const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
} // namespace wallet
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 56c676c1..abbb6c12 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -2534,7 +2534,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
return res;
}
-util::Result<CTxDestination> CWallet::GetNewDestination(const OutputType type, const std::string label)
+util::Result<CTxDestination> CWallet::GetNewDestination(const OutputType type, const std::string& label)
{
LOCK(cs_wallet);
auto spk_man = GetScriptPubKeyMan(type, /*internal=*/false);
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index 24d6b07d..d23c1a94 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -783,7 +783,7 @@ public:
*/
void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
- util::Result<CTxDestination> GetNewDestination(const OutputType type, const std::string label);
+ util::Result<CTxDestination> GetNewDestination(const OutputType type, const std::string& label);
util::Result<CTxDestination> GetNewChangeDestination(const OutputType type);
bool IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.