Commit 68d8fa3
committed
Merge #7352: perf: optimize division to uint32 to make re-target-pow calculation much faster
e8435d3 test: add regression test for faster division of base_uint<256> to uint32_t (Konstantin Akimov)
de1e56a perf: optimize division to uint32 to make re-target-pow calculation much faster (Konstantin Akimov)
Pull request description:
## Issue being fixed or feature implemented
Dash retargets difficulty every block via DarkGravityWave (24-block loop), so every header processed during sync runs several 256-bit divisions. Bitcoin only retargets once per 2016 blocks.
Every division in DGW is by a small integer (nCountBlocks+1) * nTargetTimespan (3600).
But the code routes through the only available overload, `operator/=(const base_uint&)`, which does bit-by-bit long division: ~224 iterations, each doing a full-width `div >>= 1`
During header sync up to 35% of CPU is wasted on this division on release build (accordingly `perf`):
```
20.36% d-msghand dash-qt [.] base_uint<256u>::operator>>=(unsigned int)
15.00% d-msghand dash-qt [.] base_uint<256u>::operator/=(base_uint<256u> const&)
```
## What was done?
Implemented optimization for division to unsigned values that could fit inside uint32_t.
## How Has This Been Tested?
Got GUIX build from changes in this PR and develop:
```
[develop] 5af9f57/src/qt/dash-qt -datadir=/tmp/dd -reindex
real 7m42.152s
user 7m0.396s
sys 0m3.328s
[PR] 8608a3c3bbcd/src/qt/dash-qt -datadir=/tmp/dd -reindex
real 4m17.294s
user 2m55.474s
sys 0m2.731s
```
Perf stats are updated as expected:
```
- 1.61% 0.06% d-msghand dash-qt [.] base_uint<256u>::operator/=(base_uint<256u> const&)
- 1.55% base_uint<256u>::operator/=(base_uint<256u> const&)
0.92% base_uint<256u>::operator>>=(unsigned int)
+ 1.48% 0.57% d-msghand dash-qt [.] base_uint<256u>::operator>>=(unsigned int)
```
So total header sync just a half of the time. Please note, that header sync will be slightly slower due to #7320 so overall win is smaller.
## Breaking Changes
N/A
## Checklist:
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added or updated relevant unit/integration/functional/e2e tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_
ACKs for top commit:
UdjinM6:
utACK e8435d3
Tree-SHA512: 797c446cbfad6a1eecb98bee893d530e88db7576d0d15faa36c0d9ccb6d3872313c29f977194ba42cfdedf837a7227018dc750fa87b1f376106831204b08f6503 files changed
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
75 | 93 | | |
76 | 94 | | |
77 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
| |||
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| 211 | + | |
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
364 | 377 | | |
365 | 378 | | |
366 | 379 | | |
| |||
0 commit comments