Summary
This PR optimizes CBlockIndexWorkComparator::operator() by removing 4 redundant branches.
The previous implementation used multiple separate comparisons with explicit branches for greater-than and less-than cases, resulting in unnecessary code paths.
The new implementation consolidates comparisons into single inequality checks and reduces complexity while preserving its original behavior. This change is particularly beneficial for loading blocks from files and reindexing.
Benchmarks
0taskset -c 1 ./bin/bench_bitcoin --filter="(CheckBlockIndex|LoadExternalBlockFile)" -output-csv=bench_old.csv --min-time=30000
Before:
| ns/op | op/s | err% | total | benchmark |
|---|---|---|---|---|
| 129,988.82 | 7,692.97 | 0.0% | 33.02 | CheckBlockIndex |
| 21,661,396.96 | 46.17 | 0.5% | 32.37 | LoadExternalBlockFile |
After:
| ns/op | op/s | err% | total | benchmark |
|---|---|---|---|---|
| 115,346.65 | 8,669.52 | 0.0% | 33.00 | CheckBlockIndex |
| 20,389,679.85 | 49.04 | 0.4% | 31.76 | LoadExternalBlockFile |
Compared to master:
CheckBlockIndex+12.7% fasterLoadExternalBlockFile+6.2% faster