Commit 8abf920
committed
Auto merge of #117722 - okaneco:binarysearch, r=thomcc
Refactor `binary_search_by` to use conditional moves
Refactor the if/else checking on `cmp::Ordering` variants to a "branchless" reassignment of left and right.
This change results in fewer branches and instructions.
https://rust.godbolt.org/z/698eYffTx
---
I saw consistent benchmark improvements locally. Performance of worst case seems about the same, maybe slightly faster for the L3 test.
Current
```
slice::binary_search_l1 43.00ns/iter +/- 3.00ns
slice::binary_search_l1_with_dups 25.00ns/iter +/- 0.00ns
slice::binary_search_l1_worst_case 10.00ns/iter +/- 0.00ns
slice::binary_search_l2 64.00ns/iter +/- 1.00ns
slice::binary_search_l2_with_dups 42.00ns/iter +/- 0.00ns
slice::binary_search_l2_worst_case 16.00ns/iter +/- 0.00ns
slice::binary_search_l3 132.00ns/iter +/- 2.00ns
slice::binary_search_l3_with_dups 108.00ns/iter +/- 2.00ns
slice::binary_search_l3_worst_case 33.00ns/iter +/- 3.00ns
```
This PR
```
slice::binary_search_l1 21.00ns/iter +/- 0.00ns
slice::binary_search_l1_with_dups 14.00ns/iter +/- 0.00ns
slice::binary_search_l1_worst_case 9.00ns/iter +/- 0.00ns
slice::binary_search_l2 34.00ns/iter +/- 0.00ns
slice::binary_search_l2_with_dups 23.00ns/iter +/- 0.00ns
slice::binary_search_l2_worst_case 16.00ns/iter +/- 0.00ns
slice::binary_search_l3 92.00ns/iter +/- 3.00ns
slice::binary_search_l3_with_dups 63.00ns/iter +/- 1.00ns
slice::binary_search_l3_worst_case 29.00ns/iter +/- 0.00ns
```1 file changed
+8
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
2854 | 2854 | | |
2855 | 2855 | | |
2856 | 2856 | | |
2857 | | - | |
2858 | | - | |
2859 | | - | |
2860 | | - | |
2861 | | - | |
2862 | | - | |
2863 | | - | |
2864 | | - | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
| 2862 | + | |
| 2863 | + | |
2865 | 2864 | | |
2866 | 2865 | | |
2867 | 2866 | | |
| |||
0 commit comments