Skip to content
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

Towards faster binary search #74547

Closed
LifeIsStrange opened this issue Jul 20, 2020 · 5 comments
Closed

Towards faster binary search #74547

LifeIsStrange opened this issue Jul 20, 2020 · 5 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@LifeIsStrange
Copy link

LifeIsStrange commented Jul 20, 2020

rust/src/libcore/slice/mod.rs

Lines 1565 to 1570 in 05630b0

pub fn binary_search(&self, x: &T) -> Result<usize, usize>
where
T: Ord,
{
self.binary_search_by(|p| p.cmp(x))
}

Could be improved in the same way as rust has switched it's old hashmap to the state of the art hashbrown.

It seems that there is a new state of the art Binary search in town leading up to 25% performance improvement and binary search is a foundational task.
Therefore it would be promising to investigate whether Rust could have a faster binary search implementation, the candidate being
https://news.ycombinator.com/item?id=23893366
(but maybe there are other new state of the art)

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 20, 2020
@tesuji
Copy link
Contributor

tesuji commented Jul 20, 2020

Please edit to use permanent link:

rust/src/libcore/slice/mod.rs

Lines 1565 to 1570 in 05630b0

pub fn binary_search(&self, x: &T) -> Result<usize, usize>
where
T: Ord,
{
self.binary_search_by(|p| p.cmp(x))
}

@leonardo-m
Copy link

I've just tried a quaternary search and it's slower than the std one

@jakubadamw
Copy link
Contributor

jakubadamw commented Jul 20, 2020

Binary search ought to be the most overstudied algorithm in computer science. It's important to keep that in mind when one sees a new revelation about it on Hacker News.

If one is interested in experimenting with the different n-ary search algorithms in Rust in a crate, then that sounds like an idea, but it seems out of scope for the standard library. All the more that the quaternary variant, where they report a “25%” speed-up in their tests and data set, is clearly not a “binary search” any more (though for some reason they call it a “quaternary binary search”, which is funny), so it in no way can replace the std's binary search.

@LifeIsStrange
Copy link
Author

There is also branchless Binary searches that can benefit from SIMD
https://github.com/ehrmann/branchless-binary-search

@tesuji
Copy link
Contributor

tesuji commented Aug 30, 2020

Also see #53823 : the problem is not branchless, but how easy the CPU could predict it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants