Skip to content

Commit

Permalink
rpc: reserve space for UniValue variables in blockToJSON
Browse files Browse the repository at this point in the history
- Reserving space avoid reallocation, this provide noticeable
  performance increase in verbosity level 1, 2.
  • Loading branch information
ismaelsadeeq committed Oct 30, 2024
1 parent 952463c commit f8d5d2c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
entry.pushKV("locktime", (int64_t)tx.nLockTime);

UniValue vin{UniValue::VARR};
vin.reserve(tx.vin.size());

// If available, use Undo data to calculate the fee. Note that txundo == nullptr
// for coinbase transactions and for transactions where undo data is unavailable.
Expand Down Expand Up @@ -232,6 +233,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
entry.pushKV("vin", std::move(vin));

UniValue vout(UniValue::VARR);
vout.reserve(tx.vout.size());
for (unsigned int i = 0; i < tx.vout.size(); i++) {
const CTxOut& txout = tx.vout[i];

Expand Down
1 change: 1 addition & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
result.pushKV("size", (int)::GetSerializeSize(TX_WITH_WITNESS(block)));
result.pushKV("weight", (int)::GetBlockWeight(block));
UniValue txs(UniValue::VARR);
txs.reserve(block.vtx.size());

switch (verbosity) {
case TxVerbosity::SHOW_TXID:
Expand Down

0 comments on commit f8d5d2c

Please sign in to comment.