Commit 3dc1b1e
committed
Auto merge of #127007 - krtab:improv_binary_search, r=<try>
Improve slice::binary_search_by
This PR aims to improve the performances of std::slice::binary_search.
**EDIT: The proposed implementation changed so the rest of this comment is outdated. See #127007 (comment) for an up to date presentation of the PR.**
It reduces the total instruction count for the `u32` monomorphization, but maybe more remarkably, removes 2 of the 12 instructions of the main loop (on x86).
It changes `test_binary_search_implementation_details()` so may warrant a crater run.
I will document it much more if this is shown to be interesting on benchmarks. Could we start with a timer run first?
**Before the PR**
```asm
mov eax, 1
test rsi, rsi
je .LBB0_1
mov rcx, rdx
mov rdx, rsi
mov ecx, dword ptr [rcx]
xor esi, esi
mov r8, rdx
.LBB0_3:
shr rdx
add rdx, rsi
mov r9d, dword ptr [rdi + 4*rdx]
cmp r9d, ecx
je .LBB0_4
lea r10, [rdx + 1]
cmp r9d, ecx
cmova r8, rdx
cmovb rsi, r10
mov rdx, r8
sub rdx, rsi
ja .LBB0_3
mov rdx, rsi
ret
.LBB0_1:
xor edx, edx
ret
.LBB0_4:
xor eax, eax
ret
```
**After the PR**
```asm
mov ecx, dword ptr [rdx]
xor eax, eax
xor edx, edx
.LBB1_1:
cmp rsi, 1
jbe .LBB1_2
mov r9, rsi
shr r9
lea r8, [r9 + rdx]
sub rsi, r9
cmp dword ptr [rdi + 4*r8], ecx
cmovb rdx, r8
cmova rsi, r9
jne .LBB1_1
mov rdx, r8
ret
.LBB1_2:
test rsi, rsi
je .LBB1_3
xor eax, eax
cmp dword ptr [rdi + 4*rdx], ecx
setne al
adc rdx, 0
ret
.LBB1_3:
mov eax, 1
ret
```2 files changed
+21
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2787 | 2787 | | |
2788 | 2788 | | |
2789 | 2789 | | |
| 2790 | + | |
| 2791 | + | |
| 2792 | + | |
| 2793 | + | |
| 2794 | + | |
| 2795 | + | |
| 2796 | + | |
| 2797 | + | |
| 2798 | + | |
| 2799 | + | |
| 2800 | + | |
| 2801 | + | |
2790 | 2802 | | |
2791 | | - | |
| 2803 | + | |
2792 | 2804 | | |
2793 | 2805 | | |
2794 | | - | |
| 2806 | + | |
2795 | 2807 | | |
2796 | | - | |
2797 | 2808 | | |
2798 | | - | |
| 2809 | + | |
| 2810 | + | |
| 2811 | + | |
2799 | 2812 | | |
2800 | 2813 | | |
2801 | 2814 | | |
| |||
2807 | 2820 | | |
2808 | 2821 | | |
2809 | 2822 | | |
| 2823 | + | |
2810 | 2824 | | |
2811 | 2825 | | |
2812 | 2826 | | |
2813 | 2827 | | |
2814 | 2828 | | |
2815 | 2829 | | |
2816 | 2830 | | |
2817 | | - | |
2818 | | - | |
2819 | 2831 | | |
2820 | | - | |
2821 | | - | |
2822 | | - | |
| 2832 | + | |
2823 | 2833 | | |
2824 | 2834 | | |
2825 | 2835 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| |||
0 commit comments