p2p, refactor: performance improvements to ProtectEvictionCandidatesByRatio()
Summary:
This follow-up to #21261 improves ProtectEvictionCandidatesByRatio() for better performance.
Benchmarks are added; the performance improvement is between 2x and 5x for the benchmarked cases.
The refactored code is well-covered by existing unit tests.
p2p: iterate eviction protection only on networks having candidates in ProtectEvictionCandidatesByRatio().
p2p: process more candidates per protection iteration for the usual case when some of the protected networks don't have eviction candidates, to reduce the number of iterations in ProtectEvictionCandidatesByRatio()
p2p: earlier continuation when no remaining eviction candidates in ProtectEvictionCandidatesByRatio().
With this change, if (n.count == 0) continue; will be true
if a network had candidates protected in the first iterations
and has no candidates remaining to be protected in later iterations.
This is a backport of core#22284
Depends on D11051
Test Plan:
ninja all check-all bitcoin-bench && src/bench/bitcoin-bench
Before:
| ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 308,564.89 | 3,240.81 | 0.1% | 0.00 | `EvictionProtection0Networks250Candidates` | 329,332.71 | 3,036.44 | 0.0% | 0.00 | `EvictionProtection1Networks250Candidates` | 361,197.71 | 2,768.57 | 0.1% | 0.00 | `EvictionProtection2Networks250Candidates` | 49,956.19 | 20,017.54 | 0.0% | 0.00 | `EvictionProtection3Networks050Candidates` | 109,924.79 | 9,097.13 | 0.1% | 0.00 | `EvictionProtection3Networks100Candidates` | 681,640.58 | 1,467.05 | 0.1% | 0.01 | `EvictionProtection3Networks250Candidates`
After:
| ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 81,663.94 | 12,245.31 | 0.1% | 0.00 | `EvictionProtection0Networks250Candidates` | 110,304.82 | 9,065.79 | 0.0% | 0.00 | `EvictionProtection1Networks250Candidates` | 147,154.10 | 6,795.60 | 0.1% | 0.00 | `EvictionProtection2Networks250Candidates` | 26,983.78 | 37,059.30 | 0.1% | 0.00 | `EvictionProtection3Networks050Candidates` | 69,721.16 | 14,342.85 | 0.0% | 0.00 | `EvictionProtection3Networks100Candidates` | 157,214.90 | 6,360.72 | 0.1% | 0.00 | `EvictionProtection3Networks250Candidates`
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Subscribers: Fabien
Differential Revision: https://reviews.bitcoinabc.org/D11061