From 66082ca3488e7ad78149e05631dccd09be03c961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 9 Mar 2024 20:56:19 +0100 Subject: [PATCH] Preallocate addresses in GetAddr based on nNodes > make && ./src/bench/bench_bitcoin --filter=AddrManGetAddr --min-time=1000 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` ``` --- src/addrman.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/addrman.cpp b/src/addrman.cpp index d0b820ee651..a351b6fbc57 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -818,6 +818,7 @@ std::vector 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()}; std::vector addresses; + addresses.reserve(nNodes); for (unsigned int n = 0; n < vRandom.size(); n++) { if (addresses.size() >= nNodes) break;