This PR is motivated by #30495 (comment),
It adds a reserve
member function to UniValue
and applies it within the getblock
RPC to pre-allocate memory, minimizing reallocations. This change provides a noticeable performance increase, particularly at verbosity levels 1 and 2.
Here are benchmark for this conducted on a VPS with the specs:
- 8 vCPU Cores, 24 GB RAM, 1.2 TB SSD, 32 TB Traffic
- Ubuntu 22 with Bitcoin Core on the latest master da10e0bab4a3e98868dd663af02c43b1dc8b7f4a
A script was used to retrieve 1000 blocks starting at block 840000, evaluated at verbosity levels 1, 2, and 3 using a sequential strategy. Each configuration was run in three iterations.
Master Branch
Verbosity Level 1
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 202 sec | 118 sec | 119 sec | 146 sec | 39 sec |
Verbosity Level 2
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 5004 sec | 3517 sec | 4952 sec | 4491 sec | 689 sec |
Verbosity Level 3
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 4145 sec | 4175 sec | 4187 sec | 4169 sec | 18 sec |
PR Branch with UniValue .reserve
Verbosity Level 1
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 122 sec | 105 sec | 107 sec | 111 sec | 7 sec |
Verbosity Level 2
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 3241 sec | 3272 sec | 3267 sec | 3260 sec | 14 sec |
Verbosity Level 3
Strategy | Iteration 1 | Iteration 2 | Iteration 3 | Mean | Standard Deviation |
---|---|---|---|---|---|
Sequential | 4089 sec | 4213 sec | 4202 sec | 4168 sec | 56 sec |
Reserving the space in UniValue
results in noticeable speed improvements, especially for verbosity levels 1 and 2 by 24-27% on average.