Skip to content

Commit

Permalink
Merge #14298: [REST] improve performance for JSON calls
Browse files Browse the repository at this point in the history
Summary:
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

Backport of Core [[bitcoin/bitcoin#14298 | PR14298]]

Test Plan:
My computer is apparently too fast to measure these perf improvements reliably, but I tried this pre- and post-patch:
```
ninja check
bitcoind -rest
time curl localhost:8332/rest/headers/2000/<some-blockhash>.json
time curl localhost:8332/rest/block/<some-blockhash>.json
```

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D5783
  • Loading branch information
MarcoFalke authored and ftrader committed May 25, 2020
1 parent 5fe1749 commit cb7cdd5
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/rest.cpp
Expand Up @@ -167,20 +167,25 @@ static bool rest_headers(Config &config, HTTPRequest *req,
}
}

CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader();
}

switch (rf) {
case RetFormat::BINARY: {
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
for (const CBlockIndex *pindex : headers) {
ssHeader << pindex->GetBlockHeader();
}

std::string binaryHeader = ssHeader.str();
req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryHeader);
return true;
}

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";
req->WriteHeader("Content-Type", "text/plain");
Expand Down Expand Up @@ -242,19 +247,21 @@ static bool rest_block(const Config &config, HTTPRequest *req,
}
}

CDataStream ssBlock(SER_NETWORK,
PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;

switch (rf) {
case RetFormat::BINARY: {
CDataStream ssBlock(SER_NETWORK,
PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string binaryBlock = ssBlock.str();
req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryBlock);
return true;
}

case RetFormat::HEX: {
CDataStream ssBlock(SER_NETWORK,
PROTOCOL_VERSION | RPCSerializationFlags());
ssBlock << block;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
Expand Down Expand Up @@ -391,18 +398,23 @@ static bool rest_tx(Config &config, HTTPRequest *req,
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
}

CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx;

switch (rf) {
case RetFormat::BINARY: {
CDataStream ssTx(SER_NETWORK,
PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx;

std::string binaryTx = ssTx.str();
req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryTx);
return true;
}

case RetFormat::HEX: {
CDataStream ssTx(SER_NETWORK,
PROTOCOL_VERSION | RPCSerializationFlags());
ssTx << tx;

std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex);
Expand Down

0 comments on commit cb7cdd5

Please sign in to comment.