From 60627c9f24f5c322c59abee7c8e9e03a2b2ccf5a Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Fri, 29 May 2020 22:08:19 -0400 Subject: [PATCH] Switch to insitu parsing for ZMQ-JSON; GetBlocksFast reads 13%+ faster --- src/rpc/daemon_handler.cpp | 4 ++-- src/rpc/daemon_handler.h | 2 +- src/rpc/message.cpp | 7 +++++-- src/rpc/message.h | 7 ++++--- src/rpc/rpc_handler.h | 2 +- src/rpc/zmq_server.cpp | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp index fd33318b3..0a26a4d5d 100644 --- a/src/rpc/daemon_handler.cpp +++ b/src/rpc/daemon_handler.cpp @@ -906,13 +906,13 @@ namespace rpc return true; } - epee::byte_slice DaemonHandler::handle(const std::string& request) + epee::byte_slice DaemonHandler::handle(std::string&& request) { MDEBUG("Handling RPC request: " << request); try { - FullMessage req_full(request, true); + FullMessage req_full(std::move(request), true); const std::string request_type = req_full.getRequestType(); const auto matched_handler = std::lower_bound(std::begin(handlers), std::end(handlers), request_type); diff --git a/src/rpc/daemon_handler.h b/src/rpc/daemon_handler.h index aa3470c25..31c4f3ec4 100644 --- a/src/rpc/daemon_handler.h +++ b/src/rpc/daemon_handler.h @@ -133,7 +133,7 @@ class DaemonHandler : public RpcHandler void handle(const GetOutputDistribution::Request& req, GetOutputDistribution::Response& res); - epee::byte_slice handle(const std::string& request) override final; + epee::byte_slice handle(std::string&& request) override final; private: diff --git a/src/rpc/message.cpp b/src/rpc/message.cpp index e4f17cef8..3aefbd76e 100644 --- a/src/rpc/message.cpp +++ b/src/rpc/message.cpp @@ -79,9 +79,12 @@ void Message::fromJson(const rapidjson::Value& val) GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version); } -FullMessage::FullMessage(const std::string& json_string, bool request) +FullMessage::FullMessage(std::string&& json_string, bool request) + : contents(std::move(json_string)), doc() { - doc.Parse(json_string.c_str()); + /* Insitu parsing does not copy data from `contents` to DOM, + accelerating string heavy content. */ + doc.ParseInsitu(std::addressof(contents[0])); if (doc.HasParseError() || !doc.IsObject()) { throw cryptonote::json::PARSE_FAIL(); diff --git a/src/rpc/message.h b/src/rpc/message.h index b858a5913..04bf1a111 100644 --- a/src/rpc/message.h +++ b/src/rpc/message.h @@ -72,9 +72,7 @@ namespace rpc public: ~FullMessage() { } - FullMessage(FullMessage&& rhs) noexcept : doc(std::move(rhs.doc)) { } - - FullMessage(const std::string& json_string, bool request=false); + FullMessage(std::string&& json_string, bool request=false); std::string getRequestType() const; @@ -91,10 +89,13 @@ namespace rpc private: FullMessage() = default; + FullMessage(const FullMessage&) = delete; + FullMessage& operator=(const FullMessage&) = delete; FullMessage(const std::string& request, Message* message); FullMessage(Message* message); + std::string contents; rapidjson::Document doc; }; diff --git a/src/rpc/rpc_handler.h b/src/rpc/rpc_handler.h index 97dd0598b..9757fc462 100644 --- a/src/rpc/rpc_handler.h +++ b/src/rpc/rpc_handler.h @@ -55,7 +55,7 @@ class RpcHandler RpcHandler() { } virtual ~RpcHandler() { } - virtual epee::byte_slice handle(const std::string& request) = 0; + virtual epee::byte_slice handle(std::string&& request) = 0; static boost::optional get_output_distribution(const std::function&, uint64_t&)> &f, uint64_t amount, uint64_t from_height, uint64_t to_height, const std::function &get_hash, bool cumulative, uint64_t blockchain_height); diff --git a/src/rpc/zmq_server.cpp b/src/rpc/zmq_server.cpp index 6105b7f3a..4028df96a 100644 --- a/src/rpc/zmq_server.cpp +++ b/src/rpc/zmq_server.cpp @@ -158,9 +158,9 @@ void ZmqServer::serve() if (!pub || sockets[2].revents) { - const std::string message = MONERO_UNWRAP(net::zmq::receive(rep.get(), read_flags)); + std::string message = MONERO_UNWRAP(net::zmq::receive(rep.get(), read_flags)); MDEBUG("Received RPC request: \"" << message << "\""); - epee::byte_slice response = handler.handle(message); + epee::byte_slice response = handler.handle(std::move(message)); const boost::string_ref response_view{reinterpret_cast(response.data()), response.size()}; MDEBUG("Sending RPC reply: \"" << response_view << "\"");