Skip to content

Commit 545ff92

Browse files
committed
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.
1 parent 7727603 commit 545ff92

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/rpc/client.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <rpc/client.h>
7+
#include <tinyformat.h>
78
#include <util/system.h>
89

910
#include <set>
1011
#include <stdint.h>
12+
#include <string>
13+
#include <string_view>
1114

1215
class CRPCConvertParam
1316
{
@@ -228,15 +231,15 @@ class CRPCConvertTable
228231
CRPCConvertTable();
229232

230233
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
231-
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, int param_idx)
234+
UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, int param_idx)
232235
{
233-
return members.count(std::make_pair(method, param_idx)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
236+
return members.count({method, param_idx}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
234237
}
235238

236239
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
237-
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, const std::string& param_name)
240+
UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, const std::string& param_name)
238241
{
239-
return membersByName.count(std::make_pair(method, param_name)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
242+
return membersByName.count({method, param_name}) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
240243
}
241244
};
242245

@@ -253,10 +256,10 @@ static CRPCConvertTable rpcCvtTable;
253256
/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
254257
* as well as objects and arrays.
255258
*/
256-
UniValue ParseNonRFCJSONValue(const std::string& raw)
259+
UniValue ParseNonRFCJSONValue(std::string_view raw)
257260
{
258261
UniValue parsed;
259-
if (!parsed.read(raw)) throw std::runtime_error(std::string("Error parsing JSON: ") + raw);
262+
if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw));
260263
return parsed;
261264
}
262265

@@ -265,8 +268,8 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
265268
UniValue params(UniValue::VARR);
266269

267270
for (unsigned int idx = 0; idx < strParams.size(); idx++) {
268-
const std::string& strVal = strParams[idx];
269-
params.push_back(rpcCvtTable.ArgToUniValue(strVal, strMethod, idx));
271+
std::string_view value{strParams[idx]};
272+
params.push_back(rpcCvtTable.ArgToUniValue(value, strMethod, idx));
270273
}
271274

272275
return params;
@@ -277,15 +280,15 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
277280
UniValue params(UniValue::VOBJ);
278281
UniValue positional_args{UniValue::VARR};
279282

280-
for (const std::string &s: strParams) {
283+
for (std::string_view s: strParams) {
281284
size_t pos = s.find('=');
282285
if (pos == std::string::npos) {
283286
positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
284287
continue;
285288
}
286289

287-
std::string name = s.substr(0, pos);
288-
std::string value = s.substr(pos+1);
290+
std::string name{s.substr(0, pos)};
291+
std::string_view value{s.substr(pos+1)};
289292

290293
// Intentionally overwrite earlier named values with later ones as a
291294
// convenience for scripts and command line users that want to merge

src/rpc/client.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#ifndef BITCOIN_RPC_CLIENT_H
77
#define BITCOIN_RPC_CLIENT_H
88

9+
#include <string>
10+
#include <string_view>
11+
912
#include <univalue.h>
1013

1114
/** Convert positional arguments to command-specific RPC representation */
@@ -17,6 +20,6 @@ UniValue RPCConvertNamedValues(const std::string& strMethod, const std::vector<s
1720
/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
1821
* as well as objects and arrays.
1922
*/
20-
UniValue ParseNonRFCJSONValue(const std::string& raw);
23+
UniValue ParseNonRFCJSONValue(std::string_view raw);
2124

2225
#endif // BITCOIN_RPC_CLIENT_H

src/univalue/include/univalue.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <map>
1313
#include <stdexcept>
1414
#include <string>
15+
#include <string_view>
1516
#include <type_traits>
1617
#include <vector>
1718

@@ -95,9 +96,7 @@ class UniValue {
9596

9697
bool read(const char *raw, size_t len);
9798
bool read(const char *raw) { return read(raw, strlen(raw)); }
98-
bool read(const std::string& rawStr) {
99-
return read(rawStr.data(), rawStr.size());
100-
}
99+
bool read(std::string_view raw) { return read(raw.data(), raw.size()); }
101100

102101
private:
103102
UniValue::VType typ;

0 commit comments

Comments
 (0)