Switch to insitu parsing for ZMQ-JSON; GetBlocksFast reads 13%+ faster

This commit is contained in:
Lee Clagett 2020-05-29 22:08:19 -04:00
parent fe96e66ebe
commit 60627c9f24
6 changed files with 15 additions and 11 deletions

View file

@ -906,13 +906,13 @@ namespace rpc
return true; return true;
} }
epee::byte_slice DaemonHandler::handle(const std::string& request) epee::byte_slice DaemonHandler::handle(std::string&& request)
{ {
MDEBUG("Handling RPC request: " << request); MDEBUG("Handling RPC request: " << request);
try try
{ {
FullMessage req_full(request, true); FullMessage req_full(std::move(request), true);
const std::string request_type = req_full.getRequestType(); const std::string request_type = req_full.getRequestType();
const auto matched_handler = std::lower_bound(std::begin(handlers), std::end(handlers), request_type); const auto matched_handler = std::lower_bound(std::begin(handlers), std::end(handlers), request_type);

View file

@ -133,7 +133,7 @@ class DaemonHandler : public RpcHandler
void handle(const GetOutputDistribution::Request& req, GetOutputDistribution::Response& res); 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: private:

View file

@ -79,9 +79,12 @@ void Message::fromJson(const rapidjson::Value& val)
GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version); 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()) if (doc.HasParseError() || !doc.IsObject())
{ {
throw cryptonote::json::PARSE_FAIL(); throw cryptonote::json::PARSE_FAIL();

View file

@ -72,9 +72,7 @@ namespace rpc
public: public:
~FullMessage() { } ~FullMessage() { }
FullMessage(FullMessage&& rhs) noexcept : doc(std::move(rhs.doc)) { } FullMessage(std::string&& json_string, bool request=false);
FullMessage(const std::string& json_string, bool request=false);
std::string getRequestType() const; std::string getRequestType() const;
@ -91,10 +89,13 @@ namespace rpc
private: private:
FullMessage() = default; FullMessage() = default;
FullMessage(const FullMessage&) = delete;
FullMessage& operator=(const FullMessage&) = delete;
FullMessage(const std::string& request, Message* message); FullMessage(const std::string& request, Message* message);
FullMessage(Message* message); FullMessage(Message* message);
std::string contents;
rapidjson::Document doc; rapidjson::Document doc;
}; };

View file

@ -55,7 +55,7 @@ class RpcHandler
RpcHandler() { } RpcHandler() { }
virtual ~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<output_distribution_data> static boost::optional<output_distribution_data>
get_output_distribution(const std::function<bool(uint64_t, uint64_t, uint64_t, uint64_t&, std::vector<uint64_t>&, uint64_t&)> &f, uint64_t amount, uint64_t from_height, uint64_t to_height, const std::function<crypto::hash(uint64_t)> &get_hash, bool cumulative, uint64_t blockchain_height); get_output_distribution(const std::function<bool(uint64_t, uint64_t, uint64_t, uint64_t&, std::vector<uint64_t>&, uint64_t&)> &f, uint64_t amount, uint64_t from_height, uint64_t to_height, const std::function<crypto::hash(uint64_t)> &get_hash, bool cumulative, uint64_t blockchain_height);

View file

@ -158,9 +158,9 @@ void ZmqServer::serve()
if (!pub || sockets[2].revents) 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 << "\""); 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<const char*>(response.data()), response.size()}; const boost::string_ref response_view{reinterpret_cast<const char*>(response.data()), response.size()};
MDEBUG("Sending RPC reply: \"" << response_view << "\""); MDEBUG("Sending RPC reply: \"" << response_view << "\"");