Skip to content

Commit 5d82d92

Browse files
committed
rpc: reserve space for UniValue variables in blockToJSON
- Reserving space avoid reallocation, this provide noticeable performance increase in verbosity 1.
1 parent 6a506d5 commit 5d82d92

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/core_write.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
181181
entry.pushKV("locktime", (int64_t)tx.nLockTime);
182182

183183
UniValue vin{UniValue::VARR};
184+
vin.reserve(tx.vin.size());
184185

185186
// If available, use Undo data to calculate the fee. Note that txundo == nullptr
186187
// for coinbase transactions and for transactions where undo data is unavailable.
@@ -203,6 +204,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
203204
}
204205
if (!tx.vin[i].scriptWitness.IsNull()) {
205206
UniValue txinwitness(UniValue::VARR);
207+
txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
206208
for (const auto& item : tx.vin[i].scriptWitness.stack) {
207209
txinwitness.push_back(HexStr(item));
208210
}
@@ -232,6 +234,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
232234
entry.pushKV("vin", std::move(vin));
233235

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

src/rpc/blockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
184184
result.pushKV("size", (int)::GetSerializeSize(TX_WITH_WITNESS(block)));
185185
result.pushKV("weight", (int)::GetBlockWeight(block));
186186
UniValue txs(UniValue::VARR);
187+
txs.reserve(block.vtx.size());
187188

188189
switch (verbosity) {
189190
case TxVerbosity::SHOW_TXID:

0 commit comments

Comments
 (0)