0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

refactor: use string_view for RPC named argument values

Minimize copying RPC named argument values when calling .substr() by
using std::string_view instead of std::string.
This commit is contained in:
stickies-v 2022-11-30 17:16:04 +00:00
parent 7727603e44
commit 545ff924ab
No known key found for this signature in database
GPG key ID: 5CB1CE6E5E66A757
3 changed files with 20 additions and 15 deletions

View file

@ -4,10 +4,13 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <rpc/client.h> #include <rpc/client.h>
#include <tinyformat.h>
#include <util/system.h> #include <util/system.h>
#include <set> #include <set>
#include <stdint.h> #include <stdint.h>
#include <string>
#include <string_view>
class CRPCConvertParam class CRPCConvertParam
{ {
@ -228,15 +231,15 @@ public:
CRPCConvertTable(); CRPCConvertTable();
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, int param_idx) UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, int param_idx)
{ {
return members.count(std::make_pair(method, param_idx)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value; return members.count({method, param_idx}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
} }
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, const std::string& param_name) UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, const std::string& param_name)
{ {
return membersByName.count(std::make_pair(method, param_name)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value; return membersByName.count({method, param_name}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
} }
}; };
@ -253,10 +256,10 @@ static CRPCConvertTable rpcCvtTable;
/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
* as well as objects and arrays. * as well as objects and arrays.
*/ */
UniValue ParseNonRFCJSONValue(const std::string& raw) UniValue ParseNonRFCJSONValue(std::string_view raw)
{ {
UniValue parsed; UniValue parsed;
if (!parsed.read(raw)) throw std::runtime_error(std::string("Error parsing JSON: ") + raw); if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw));
return parsed; return parsed;
} }
@ -265,8 +268,8 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
UniValue params(UniValue::VARR); UniValue params(UniValue::VARR);
for (unsigned int idx = 0; idx < strParams.size(); idx++) { for (unsigned int idx = 0; idx < strParams.size(); idx++) {
const std::string& strVal = strParams[idx]; std::string_view value{strParams[idx]};
params.push_back(rpcCvtTable.ArgToUniValue(strVal, strMethod, idx)); params.push_back(rpcCvtTable.ArgToUniValue(value, strMethod, idx));
} }
return params; return params;
@ -277,15 +280,15 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
UniValue params(UniValue::VOBJ); UniValue params(UniValue::VOBJ);
UniValue positional_args{UniValue::VARR}; UniValue positional_args{UniValue::VARR};
for (const std::string &s: strParams) { for (std::string_view s: strParams) {
size_t pos = s.find('='); size_t pos = s.find('=');
if (pos == std::string::npos) { if (pos == std::string::npos) {
positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size())); positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
continue; continue;
} }
std::string name = s.substr(0, pos); std::string name{s.substr(0, pos)};
std::string value = s.substr(pos+1); std::string_view value{s.substr(pos+1)};
// Intentionally overwrite earlier named values with later ones as a // Intentionally overwrite earlier named values with later ones as a
// convenience for scripts and command line users that want to merge // convenience for scripts and command line users that want to merge

View file

@ -6,6 +6,9 @@
#ifndef BITCOIN_RPC_CLIENT_H #ifndef BITCOIN_RPC_CLIENT_H
#define BITCOIN_RPC_CLIENT_H #define BITCOIN_RPC_CLIENT_H
#include <string>
#include <string_view>
#include <univalue.h> #include <univalue.h>
/** Convert positional arguments to command-specific RPC representation */ /** Convert positional arguments to command-specific RPC representation */
@ -17,6 +20,6 @@ UniValue RPCConvertNamedValues(const std::string& strMethod, const std::vector<s
/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
* as well as objects and arrays. * as well as objects and arrays.
*/ */
UniValue ParseNonRFCJSONValue(const std::string& raw); UniValue ParseNonRFCJSONValue(std::string_view raw);
#endif // BITCOIN_RPC_CLIENT_H #endif // BITCOIN_RPC_CLIENT_H

View file

@ -12,6 +12,7 @@
#include <map> #include <map>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
@ -95,9 +96,7 @@ public:
bool read(const char *raw, size_t len); bool read(const char *raw, size_t len);
bool read(const char *raw) { return read(raw, strlen(raw)); } bool read(const char *raw) { return read(raw, strlen(raw)); }
bool read(const std::string& rawStr) { bool read(std::string_view raw) { return read(raw.data(), raw.size()); }
return read(rawStr.data(), rawStr.size());
}
private: private:
UniValue::VType typ; UniValue::VType typ;