-
Notifications
You must be signed in to change notification settings - Fork 37.7k
p2p, refactor: performance improvements to ProtectEvictionCandidatesByRatio() #22284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
p2p, refactor: performance improvements to ProtectEvictionCandidatesByRatio() #22284
Conversation
acc3f69
to
ab29933
Compare
Added benchmarks. This pull speeds up
master | ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 5,403,043,444.00 | 0.19 | 1.8% | 59.74 | `EvictionProtection1Network250Candidates`
| 2,946,547,379.00 | 0.34 | 2.7% | 33.45 | `EvictionProtection3Networks050Candidates`
| 2,798,691,071.00 | 0.36 | 2.2% | 30.79 | `EvictionProtection3Networks100Candidates`
| 7,288,364,370.00 | 0.14 | 0.7% | 80.16 | `EvictionProtection3Networks250Candidates` this PR | ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 1,183,198,205.00 | 0.85 | 1.2% | 13.12 | `EvictionProtection1Network250Candidates`
| 1,151,709,186.00 | 0.87 | 1.7% | 12.67 | `EvictionProtection3Networks050Candidates`
| 1,261,170,266.00 | 0.79 | 2.9% | 14.26 | `EvictionProtection3Networks100Candidates`
| 1,157,625,808.00 | 0.86 | 1.0% | 12.95 | `EvictionProtection3Networks250Candidates` (intermediate commits) p2p: iterate over eviction candidates once instead of thrice | ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 3,952,529,089.00 | 0.25 | 1.8% | 43.47 | `EvictionProtection1Network250Candidates`
| 2,239,435,919.00 | 0.45 | 3.8% | 25.85 | `EvictionProtection3Networks050Candidates`
| 2,060,389,113.00 | 0.49 | 1.9% | 22.85 | `EvictionProtection3Networks100Candidates`
| 5,258,681,959.00 | 0.19 | 1.0% | 58.26 | `EvictionProtection3Networks250Candidates` p2p: iterate eviction protection only on networks having candidates | ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 1,224,597,989.00 | 0.82 | 2.2% | 13.60 | `EvictionProtection1Network250Candidates` |
Tested on macOs 11.4 and this PR indeed optimize it by 1.3x to 5x. Test resultOn 272f327(master with benchmarks):
This PR:
Not sure if it is an issue on my side but I had to test with |
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK ab29933
This is quite a performance improvement and is a clear change which is easy to follow. My benchmark tests confirm the performance improvement. Thanks for working on this improvement and for adding additional benchmark coverage.
Ran benchmarks upon each intermediate commit to test performance improvement at every stage. Cherry-picked each commit on top of current master and ran unit tests which each commit cherry-picked.
Master
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 2,283,211,027.00 | 0.44 | 0.1% | 25.13 | `EvictionProtection1Network250Candidates`
| 1,417,424,933.00 | 0.71 | 0.1% | 15.59 | `EvictionProtection3Networks050Candidates`
| 1,293,719,559.00 | 0.77 | 0.1% | 14.25 | `EvictionProtection3Networks100Candidates`
| 3,350,000,724.00 | 0.30 | 0.1% | 36.88 | `EvictionProtection3Networks250Candidates`
PR
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 778,011,162.00 | 1.29 | 0.3% | 8.56 | `EvictionProtection1Network250Candidates`
| 811,555,934.00 | 1.23 | 0.1% | 8.93 | `EvictionProtection3Networks050Candidates`
| 806,194,432.00 | 1.24 | 0.1% | 8.87 | `EvictionProtection3Networks100Candidates`
| 776,191,251.00 | 1.29 | 0.1% | 8.54 | `EvictionProtection3Networks250Candidates`
Notes on commits
- 272f327
- Definition of test is appropriate and effective in terms of benchmarking what we care about
- 99a25fb
- the performance boost is from moving away from looping through each network and then interating through all
eviction_candidates
-> iterate througheviction_candidates
once; for each candidate we iterate through add count to appropriate network - Still appropriately counts eviction candidates per network
- Performance improvement at this stage is very modest compared to
master+272f327fbfcb8bb61f8e1c9d65c665372cd60976
- the performance boost is from moving away from looping through each network and then interating through all
- 06b898c
- uses lambda expression in
std::count_if
to efficiently determine networks we have candidates from - PR description states that "... increase the number protected per peer iteration", this would only come into play if there was a descrease in the number of networks to protect because we detected there were no candidates in that network. If you have to retouch, can you document this more clearly?
- this and 99a25fb make up the bulk of the performance improvement
- uses lambda expression in
- 4785343
- makes sense 🥃
- ab29933
- Is an additional performance improvement
Concept ACK |
d8513fe doc: update doc/benchmarking.md (Jon Atack) 84e2d5b bench: bench_bitcoin.cpp help fixups (Jon Atack) 10f4ce2 bench: bench.h fixes and improvements (Jon Atack) Pull request description: Fixups and updates I noticed while writing benchmarks for #22284. ACKs for top commit: za-kk: ACK d8513fe theStack: ACK d8513fe 🚤 Tree-SHA512: d494956b5d6a3329e98e8b6f4405a10613b8fce51a04bbf4493d8b3497b8d5b177c1a9a3eeb828796eb4edb92b0ace769595151e223671c0dc8f09bcf631ebb5
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
ab29933
to
524aa5d
Compare
Thank you @vasild, @jarolrod, @klementtan and @theStack; feedback taken (and rebased, apologies). |
Tested on 524aa5d
Bench ResultsNote: Results are in reverse chronological order of commits Master + Bench(edebbe7) results
c0d658f results
d2bb623 results
edc59d0 resutls``` | ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 7,874.32 | 126,995.09 | 0.9% | 0.01 | `EvictionProtection1Network250Candidates` | 5,736.20 | 174,331.44 | 1.0% | 0.01 | `EvictionProtection3Networks050Candidates` | 13,836.98 | 72,270.11 | 9.0% | 0.02 | 〰️ `EvictionProtection3Networks100Candidates` (Unstable with ~100.0 iters. Increase `minEpochIterations` to e.g. 1000) | 98,370.21 | 10,165.68 | 5.4% | 0.10 | 〰️ `EvictionProtection3Networks250Candidates` (Unstable with ~100.0 iters. Increase `minEpochIterations` to e.g. 1000) ```524aa5d results
|
Thanks for testing @klementtan!
Thanks for pointing this out. The benchmarks don't test it. Will look at adding a bench with no protected networks.
This commit doesn't help performance
Thanks for reporting. The benchmark was sped up in the last push, but am looking at maybe increasing the iterations to improve this. |
524aa5d
to
4bef482
Compare
Updated per
benchmark results, CPU 2.50GHz, Turbo off, performance mode, Debian Clang 11 non-debug build, lowest |
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
in ProtectEvictionCandidatesByRatio(). Thank you to Vasil Dimov, whose suggestions during a post-merge discussion about PR 21261 reminded me that I had done this in earlier versions of the PR, e.g. commits like ef411cd. Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
for the usual case when some of the protected networks don't have eviction candidates, to reduce the number of iterations in ProtectEvictionCandidatesByRatio(). Picks up an idea in ef411cd that I had dropped.
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. Co-authored-by: Jon Atack <jon@atack.com>
4bef482
to
b1d905c
Compare
Tested and code review ACK b1d905c. Also no longer seeing Bench results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK b1d905c
base (5adb064 bench: add peer eviction protection benchmarks)
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
15,636.87 | 63,951.43 | 0.6% | 0.17 | EvictionProtection0Networks250Candidates |
12,885.39 | 77,607.27 | 0.5% | 0.14 | EvictionProtection1Networks250Candidates |
35,732.17 | 27,985.99 | 0.3% | 0.39 | EvictionProtection2Networks250Candidates |
5,514.20 | 181,349.88 | 1.9% | 0.06 | EvictionProtection3Networks050Candidates |
12,243.04 | 81,679.08 | 0.2% | 0.13 | EvictionProtection3Networks100Candidates |
76,015.27 | 13,155.25 | 0.4% | 0.85 | EvictionProtection3Networks250Candidates |
all improvements (b1d905c p2p: earlier continuation when no remaining eviction candidates)
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
10,587.04 | 94,455.10 | 0.7% | 0.12 | EvictionProtection0Networks250Candidates |
8,046.60 | 124,276.05 | 0.8% | 0.09 | EvictionProtection1Networks250Candidates |
12,479.92 | 80,128.74 | 1.0% | 0.14 | EvictionProtection2Networks250Candidates |
2,784.53 | 359,127.39 | 4.3% | 0.03 | EvictionProtection3Networks050Candidates |
8,286.96 | 120,671.54 | 0.4% | 0.09 | EvictionProtection3Networks100Candidates |
18,450.28 | 54,199.71 | 0.7% | 0.20 | EvictionProtection3Networks250Candidates |
Benchmarks complete in ~2 seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK b1d905c
The way this has progressed since my last review makes sense. This is still a performance and improvement and code looks good.
I do still run into instability:
Unstable with ~1,001.0 iters. Increase `minEpochIterations` to e.g. 10010
Not sure what to make of it because I'm running the macOS 12
Beta (not a super stable system overall). Just wanted to document that I am running into that, I will have time later today to try on other systems. If no one else is running into it, let's assume it's my system.
The warning is flaky though, here's the results of my runs where I didn't run into the warning:
Base: Master + (566357f + 5adb064)
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
18,439.40 | 54,231.71 | 4.0% | 0.21 | EvictionProtection0Networks250Candidates |
16,029.50 | 62,384.99 | 3.1% | 0.18 | EvictionProtection1Networks250Candidates |
43,125.31 | 23,188.24 | 2.6% | 0.47 | EvictionProtection2Networks250Candidates |
7,225.39 | 138,400.78 | 3.1% | 0.08 | EvictionProtection3Networks050Candidates |
15,869.58 | 63,013.65 | 3.5% | 0.18 | EvictionProtection3Networks100Candidates |
97,586.25 | 10,247.35 | 1.1% | 1.06 | EvictionProtection3Networks250Candidates |
All Improvements: b1d905c
ns/op | op/s | err% | total | benchmark |
---|---|---|---|---|
12,764.72 | 78,340.94 | 2.1% | 0.14 | EvictionProtection0Networks250Candidates |
9,367.04 | 106,757.28 | 3.0% | 0.10 | EvictionProtection1Networks250Candidates |
14,846.86 | 67,354.33 | 1.4% | 0.16 | EvictionProtection2Networks250Candidates |
3,493.85 | 286,217.28 | 4.8% | 0.04 | EvictionProtection3Networks050Candidates |
10,208.73 | 97,955.35 | 2.0% | 0.11 | EvictionProtection3Networks100Candidates |
22,498.62 | 44,447.18 | 0.9% | 0.25 | EvictionProtection3Networks250Candidates |
Yes, I tuned my system and yet still see that most of the time on at least half of the benchmarks when running all of them with |
…yRatio() 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 [[bitcoin/bitcoin#22284 | 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
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 (CPU 2.50GHz, Turbo off, performance mode, Debian Clang 11 non-debug build).
The refactored code is well-covered by existing unit tests and also a fuzzer.
$ ./src/test/test_bitcoin -t net_peer_eviction_tests
$ FUZZ=node_eviction ./src/test/fuzz/fuzz ../qa-assets/fuzz_seed_corpus/node_eviction