Skip to content

Commit df6e375

Browse files
pinheadmzryanofsky
authored andcommitted
rpc: Avoid copies in JSONRPCReplyObj()
Change parameters from const references to values, so they can be moved into the reply instead of copied. Also update callers to move instead of copy.
1 parent 09416f9 commit df6e375

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed

src/bitcoin-cli.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class AddrinfoRequestHandler : public BaseRequestHandler
302302
}
303303
addresses.pushKV("total", total);
304304
result.pushKV("addresses_known", addresses);
305-
return JSONRPCReplyObj(result, NullUniValue, 1);
305+
return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
306306
}
307307
};
308308

@@ -371,7 +371,7 @@ class GetinfoRequestHandler: public BaseRequestHandler
371371
}
372372
result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]);
373373
result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]);
374-
return JSONRPCReplyObj(result, NullUniValue, 1);
374+
return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
375375
}
376376
};
377377

@@ -709,7 +709,7 @@ class GenerateToAddressRequestHandler : public BaseRequestHandler
709709
UniValue result(UniValue::VOBJ);
710710
result.pushKV("address", address_str);
711711
result.pushKV("blocks", reply.get_obj()["result"]);
712-
return JSONRPCReplyObj(result, NullUniValue, 1);
712+
return JSONRPCReplyObj(std::move(result), NullUniValue, 1);
713713
}
714714
protected:
715715
std::string address_str;

src/httprpc.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static std::vector<std::vector<std::string>> g_rpcauth;
7373
static std::map<std::string, std::set<std::string>> g_rpc_whitelist;
7474
static bool g_rpc_whitelist_default = false;
7575

76-
static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const UniValue& id)
76+
static void JSONErrorReply(HTTPRequest* req, UniValue objError, UniValue id)
7777
{
7878
// Send error reply from json-rpc error object
7979
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
@@ -84,7 +84,7 @@ static void JSONErrorReply(HTTPRequest* req, const UniValue& objError, const Uni
8484
else if (code == RPC_METHOD_NOT_FOUND)
8585
nStatus = HTTP_NOT_FOUND;
8686

87-
std::string strReply = JSONRPCReply(NullUniValue, objError, id);
87+
std::string strReply = JSONRPCReplyObj(NullUniValue, std::move(objError), std::move(id)).write() + "\n";
8888

8989
req->WriteHeader("Content-Type", "application/json");
9090
req->WriteReply(nStatus, strReply);
@@ -203,7 +203,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
203203
UniValue result = tableRPC.execute(jreq);
204204

205205
// Send reply
206-
strReply = JSONRPCReply(result, NullUniValue, jreq.id);
206+
strReply = JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id).write() + "\n";
207207

208208
// array of requests
209209
} else if (valRequest.isArray()) {
@@ -230,8 +230,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
230230

231231
req->WriteHeader("Content-Type", "application/json");
232232
req->WriteReply(HTTP_OK, strReply);
233-
} catch (const UniValue& objError) {
234-
JSONErrorReply(req, objError, jreq.id);
233+
} catch (UniValue& e) {
234+
JSONErrorReply(req, std::move(e), jreq.id);
235235
return false;
236236
} catch (const std::exception& e) {
237237
JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);

src/rpc/request.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,18 @@ UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params,
3737
return request;
3838
}
3939

40-
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
40+
UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id)
4141
{
4242
UniValue reply(UniValue::VOBJ);
4343
if (!error.isNull())
4444
reply.pushKV("result", NullUniValue);
4545
else
46-
reply.pushKV("result", result);
47-
reply.pushKV("error", error);
48-
reply.pushKV("id", id);
46+
reply.pushKV("result", std::move(result));
47+
reply.pushKV("error", std::move(error));
48+
reply.pushKV("id", std::move(id));
4949
return reply;
5050
}
5151

52-
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
53-
{
54-
UniValue reply = JSONRPCReplyObj(result, error, id);
55-
return reply.write() + "\n";
56-
}
57-
5852
UniValue JSONRPCError(int code, const std::string& message)
5953
{
6054
UniValue error(UniValue::VOBJ);

src/rpc/request.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#include <univalue.h>
1313

1414
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
15-
UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id);
16-
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
15+
UniValue JSONRPCReplyObj(UniValue result, UniValue error, UniValue id);
1716
UniValue JSONRPCError(int code, const std::string& message);
1817

1918
/** Generate a new RPC authentication cookie and write it to disk */

src/rpc/server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
366366
jreq.parse(req);
367367

368368
UniValue result = tableRPC.execute(jreq);
369-
rpc_result = JSONRPCReplyObj(result, NullUniValue, jreq.id);
369+
rpc_result = JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id);
370370
}
371-
catch (const UniValue& objError)
371+
catch (UniValue& e)
372372
{
373-
rpc_result = JSONRPCReplyObj(NullUniValue, objError, jreq.id);
373+
rpc_result = JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id);
374374
}
375375
catch (const std::exception& e)
376376
{

0 commit comments

Comments
 (0)