Merge #14298: [REST] improve performance for JSON calls

30973e9844 [REST] improve performance for JSON calls (Antoine Le Calvez)

Pull request description:

  JSON calls do not use the raw data generated for the .bin and .hex calls.

  By moving the raw data creation into the .bin and .hex switch branches, JSON calls' performance is improved.

  Light benchmarking indicates that fetching 2000 JSON headers is ~25% faster, fetching large JSON blocks is ~4% faster.

Tree-SHA512: 433552c89bac2469d041b48a4a991d5443e4026a3ad7dc5621685386029f22826484218642fa5130c268349a55524ecbc4e30d64c867bd6632e0edd24370cf11
This commit is contained in:
MarcoFalke 2018-09-24 07:48:52 -04:00
commit f5035f9d0f
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25

View file

@ -157,13 +157,13 @@ static bool rest_headers(HTTPRequest* req,
} }
} }
switch (rf) {
case RetFormat::BINARY: {
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) { for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader(); ssHeader << pindex->GetBlockHeader();
} }
switch (rf) {
case RetFormat::BINARY: {
std::string binaryHeader = ssHeader.str(); std::string binaryHeader = ssHeader.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryHeader); req->WriteReply(HTTP_OK, binaryHeader);
@ -171,6 +171,11 @@ static bool rest_headers(HTTPRequest* req,
} }
case RetFormat::HEX: { case RetFormat::HEX: {
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader();
}
std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n"; std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
@ -224,11 +229,10 @@ static bool rest_block(HTTPRequest* req,
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
} }
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RetFormat::BINARY: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string binaryBlock = ssBlock.str(); std::string binaryBlock = ssBlock.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryBlock); req->WriteReply(HTTP_OK, binaryBlock);
@ -236,6 +240,8 @@ static bool rest_block(HTTPRequest* req,
} }
case RetFormat::HEX: { case RetFormat::HEX: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
@ -360,11 +366,11 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true)) if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true))
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
switch (rf) {
case RetFormat::BINARY: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx; ssTx << tx;
switch (rf) {
case RetFormat::BINARY: {
std::string binaryTx = ssTx.str(); std::string binaryTx = ssTx.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryTx); req->WriteReply(HTTP_OK, binaryTx);
@ -372,6 +378,9 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
} }
case RetFormat::HEX: { case RetFormat::HEX: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx;
std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n"; std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);