Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,23 @@ impl<T> SliceExt for [T] {
fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize> where
F: FnMut(&T) -> Ordering
{
let mut base : usize = 0;
let mut lim : usize = self.len();
let mut base = 0usize;
let mut s = self;

while lim != 0 {
let ix = base + (lim >> 1);
match f(&self[ix]) {
Equal => return Ok(ix),
loop {
let (head, tail) = s.split_at(s.len() >> 1);
if tail.is_empty() {
return Err(base)
}
match f(&tail[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use split_first here instead? It will do the job of .is_empty(), getting the first element, and slicing out the &tail[1..], all in one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler adds another branch, I'm not sure why.

assembly diff https://www.diffchecker.com/5p8siwcp

        if let Some((pivot, rest)) = tail.split_first() {
            match f(pivot) {
                Equal => return Ok(base + head.len()),
                Less => {
                    base += head.len() + 1;
                    s = rest;
                }
                Greater => s = head,
            }
        } else {
            return Err(base)
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranma42 do you know what's the case here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know why the code should not be the same.
Looking at the IR, the LLVM basic block that generates that jump seems to be

"_ZN5slice12_$u5b$T$u5d$8split_at21h15129678983694567549E.exit": ; preds = %loop_body
  %5 = getelementptr inbounds i32, i32* %s.sroa.0.0.ph, i32 %3
  %6 = icmp eq i32 %s.sroa.7.0, %3
  %switchtmp = icmp eq i32* %5, null
  %or.cond = or i1 %6, %switchtmp
  br i1 %or.cond, label %match_case, label %match_case4

Less => {
base = ix + 1;
lim -= 1;
base += head.len() + 1;
s = &tail[1..];
}
Greater => ()
Greater => s = head,
Equal => return Ok(base + head.len()),
}
lim >>= 1;
}
Err(base)
}

#[inline]
Expand Down
6 changes: 1 addition & 5 deletions src/libcoretest/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
use core::result::Result::{Ok, Err};

#[test]
fn binary_search_not_found() {
fn test_binary_search() {
let b = [1, 2, 4, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&6)) == Ok(3));
let b = [1, 2, 4, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&5)) == Err(3));
let b = [1, 2, 4, 6, 7, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&6)) == Ok(3));
let b = [1, 2, 4, 6, 7, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&5)) == Err(3));
let b = [1, 2, 4, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&8)) == Ok(4));
let b = [1, 2, 4, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&7)) == Err(4));
let b = [1, 2, 4, 6, 7, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&8)) == Ok(5));
let b = [1, 2, 4, 5, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&7)) == Err(5));
let b = [1, 2, 4, 5, 6, 8, 9];
assert!(b.binary_search_by(|v| v.cmp(&0)) == Err(0));
let b = [1, 2, 4, 5, 6, 8];
assert!(b.binary_search_by(|v| v.cmp(&9)) == Err(6));
Expand Down