Skip to content

Commit 30973e9

Browse files
committed
[REST] improve performance for JSON calls
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 become faster.
1 parent 920c090 commit 30973e9

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/rest.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,25 @@ static bool rest_headers(HTTPRequest* req,
157157
}
158158
}
159159

160-
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
161-
for (const CBlockIndex *pindex : headers) {
162-
ssHeader << pindex->GetBlockHeader();
163-
}
164-
165160
switch (rf) {
166161
case RetFormat::BINARY: {
162+
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
163+
for (const CBlockIndex *pindex : headers) {
164+
ssHeader << pindex->GetBlockHeader();
165+
}
166+
167167
std::string binaryHeader = ssHeader.str();
168168
req->WriteHeader("Content-Type", "application/octet-stream");
169169
req->WriteReply(HTTP_OK, binaryHeader);
170170
return true;
171171
}
172172

173173
case RetFormat::HEX: {
174+
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
175+
for (const CBlockIndex *pindex : headers) {
176+
ssHeader << pindex->GetBlockHeader();
177+
}
178+
174179
std::string strHex = HexStr(ssHeader.begin(), ssHeader.end()) + "\n";
175180
req->WriteHeader("Content-Type", "text/plain");
176181
req->WriteReply(HTTP_OK, strHex);
@@ -224,18 +229,19 @@ static bool rest_block(HTTPRequest* req,
224229
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
225230
}
226231

227-
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
228-
ssBlock << block;
229-
230232
switch (rf) {
231233
case RetFormat::BINARY: {
234+
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
235+
ssBlock << block;
232236
std::string binaryBlock = ssBlock.str();
233237
req->WriteHeader("Content-Type", "application/octet-stream");
234238
req->WriteReply(HTTP_OK, binaryBlock);
235239
return true;
236240
}
237241

238242
case RetFormat::HEX: {
243+
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
244+
ssBlock << block;
239245
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n";
240246
req->WriteHeader("Content-Type", "text/plain");
241247
req->WriteReply(HTTP_OK, strHex);
@@ -360,18 +366,21 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
360366
if (!GetTransaction(hash, tx, Params().GetConsensus(), hashBlock, true))
361367
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
362368

363-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
364-
ssTx << tx;
365-
366369
switch (rf) {
367370
case RetFormat::BINARY: {
371+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
372+
ssTx << tx;
373+
368374
std::string binaryTx = ssTx.str();
369375
req->WriteHeader("Content-Type", "application/octet-stream");
370376
req->WriteReply(HTTP_OK, binaryTx);
371377
return true;
372378
}
373379

374380
case RetFormat::HEX: {
381+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
382+
ssTx << tx;
383+
375384
std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n";
376385
req->WriteHeader("Content-Type", "text/plain");
377386
req->WriteReply(HTTP_OK, strHex);

0 commit comments

Comments
 (0)