Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#29608: optimization: Preallocate addresses in G…
Browse files Browse the repository at this point in the history
…etAddr based on nNodes

66082ca Preallocate addresses in GetAddr based on nNodes (Lőrinc)

Pull request description:

  The reserve method optimizes memory allocation by preallocating space for the expected number of elements (nNodes), reducing reallocations and improving performance. The upper bound ensures efficient memory usage based on the input constraints.

  before:
  ```
  |               ns/op |                op/s |    err% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------:|:----------
  |           76,852.79 |           13,011.89 |    0.4% |      1.07 | `AddrManGetAddr`
  |           76,598.21 |           13,055.14 |    0.2% |      1.07 | `AddrManGetAddr`
  |           76,296.32 |           13,106.79 |    0.1% |      1.07 | `AddrManGetAddr`
  ```

  after:
  ```
  |               ns/op |                op/s |    err% |     total | benchmark
  |--------------------:|--------------------:|--------:|----------:|:----------
  |           65,966.97 |           15,159.10 |    0.3% |      1.07 | `AddrManGetAddr`
  |           66,075.40 |           15,134.23 |    0.2% |      1.06 | `AddrManGetAddr`
  |           66,306.34 |           15,081.51 |    0.3% |      1.06 | `AddrManGetAddr`
  ```

ACKs for top commit:
  stickies-v:
    ACK 66082ca
  vasild:
    ACK 66082ca

Tree-SHA512: 1175cff250d9c52ed042e8807ddc2afd64a806e6f2195b5c648752869ff3beec0be8a8cbd7ab6ba35cd7077d79b88a380da6c6e244f5549f98cdd472808b6d8f
  • Loading branch information
fanquake committed Oct 25, 2024
2 parents b95adf0 + 66082ca commit d4abaf8
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
// gather a list of random nodes, skipping those of low quality
const auto now{Now<NodeSeconds>()};
std::vector<CAddress> addresses;
addresses.reserve(nNodes);
for (unsigned int n = 0; n < vRandom.size(); n++) {
if (addresses.size() >= nNodes)
break;
Expand Down

0 comments on commit d4abaf8

Please sign in to comment.