Skip to content

Faster midpoint calculation for binary_search_by - #128236

Closed
Amanieu wants to merge 1 commit into
rust-lang:masterfrom
Amanieu:opt-binary_search
Closed

Faster midpoint calculation for binary_search_by#128236
Amanieu wants to merge 1 commit into
rust-lang:masterfrom
Amanieu:opt-binary_search

Conversation

@Amanieu

Copy link
Copy Markdown
Member

This takes advantage of the fact that slices can only have a maximum of isize::MAX elements, which means that the midpoint can be calculated using (left + right) / 2 instead of left + (right - left) / 2.

The old calculation is kept only for ZST slices which can have up to usize::MAX elements.

Comparison with previous version: https://rust.godbolt.org/z/YccscWoqv

This takes advantage of the fact that slices can only have a maximum of
`isize::MAX` elements, which means that the midpoint can be calculated
using `(left + right) / 2` instead of `left + (right - left) / 2`.
The old calculation is kept only for ZST slices which can have up to
`usize::MAX` elements.
Comparison with previous version: https://rust.godbolt.org/z/YccscWoqv
@rustbot

Copy link
Copy Markdown
Collaborator

r? @Nilstrieb

rustbot has assigned @Nilstrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 26, 2024
@DaniPopes

Copy link
Copy Markdown
Contributor

CC similar attempt #127007

@Amanieu

Copy link
Copy Markdown
MemberAuthor

Actually, I've been doing more tests and I get much better results by just reverting to the original implementation from #45333 + forcing LLVM to emit CMOVs.

@AmanieuAmanieu closed this Jul 26, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@Amanieu@rustbot@DaniPopes@Noratrieb