Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Optimize: core slice binary_search_by #141097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from

Conversation

RoDmitry
Copy link

@RoDmitry RoDmitry commented May 16, 2025

I have found a more optimal algorithm for the binary search. I have ran benchmarks and it works faster than the previous one. Also I have looked at the compiled assembly, with and without an early return, and it looks good either way. And benchmarks show that it's faster with an early return. Looks strange for me that "CPU can reliably predict the loop count" based on the mathematical operation as it was before (substraction). But I don't know much about branch prediction, so feel free to improve it if possible.

Also I would like to know, is there any attitude towards optimizations using SIMD? Can I write platform specific SIMD intrinsics, or what should I use if I would like to contribute SIMD code optimizations? Is there already any SIMD code optimizations in core/std, I could look at as an example?

P.S. It's my first contribution here.

@rustbot
Copy link
Collaborator

rustbot commented May 16, 2025

r? @thomcc

rustbot has assigned @thomcc.
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

@rustbot rustbot 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 May 16, 2025
@rust-log-analyzer

This comment has been minimized.

@RoDmitry RoDmitry force-pushed the optimize_slice_bsb branch from 512dda9 to d2c7ac8 Compare May 16, 2025 17:22
@rust-log-analyzer

This comment has been minimized.

@the8472
Copy link
Member

the8472 commented May 16, 2025

There have been many changes and attempts to binary_search.

Some versions of this look quite like yours, so you'll have to justify this change by a more detailed analysis and benchmark results. std already has some that you can try to run.
Which implementation performs best also depends on the exact types, the comparison function and the distribution of the values being used for benchmarking.

Also I would like to know, is there any attitude towards optimizations using SIMD? Can I write platform specific SIMD intrinsics, or what should I use if I would like to contribute SIMD code optimizations? Is there already any SIMD code optimizations in core/std, I could look at as an example?

That's possible, but only worthwhile in a few cases where good results can be achieved with the baseline SIMD instructions available on tier1 platforms, e.g. SSE2 on x86-64-linux, since runtime detection isn't available in core.
So we only have a few of those and mostly rely on autovectorization. But you can look at #103779 for an example.

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented May 16, 2025

I have ran benchmarks and it works faster than the previous one.

Which benchmarks? Do they happen to look up the same value in the same slice repeatedly? Or otherwise have predictable patterns in the outcome of the match cmp that this change introduces? I wouldn’t be surprised if this change will look like a performance improvement for such benchmarks, but that’s not representative for many real world uses of binary search.

@RoDmitry RoDmitry force-pushed the optimize_slice_bsb branch from d2c7ac8 to def517a Compare May 16, 2025 18:09
@rust-log-analyzer

This comment has been minimized.

@RoDmitry
Copy link
Author

I agree that you need a more detailed analysis and benchmark results. Have found benchmarks in library/coretests/benches/slice.rs. Will try them later.

@RoDmitry RoDmitry force-pushed the optimize_slice_bsb branch 2 times, most recently from d27f18f to 08464b5 Compare May 16, 2025 23:51
@rust-log-analyzer

This comment has been minimized.

@RoDmitry RoDmitry force-pushed the optimize_slice_bsb branch from 08464b5 to b005d21 Compare May 17, 2025 00:16
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.