rust/library/core/tests
bors caca2121ff Auto merge of #74024 - Folyd:master, r=m-ou-se
Improve slice.binary_search_by()'s best-case performance to O(1)

This PR aimed to improve the [slice.binary_search_by()](https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search_by)'s best-case performance to O(1).

# Noticed

I don't know why the docs of `binary_search_by` said `"If there are multiple matches, then any one of the matches could be returned."`, but the implementation isn't the same thing. Actually, it returns the **last one** if multiple matches found.

Then we got two options:

## If returns the last one is the correct or desired result

Then I can rectify the docs and revert my changes.

## If the docs are correct or desired result

Then my changes can be merged after fully reviewed.

However, if my PR gets merged, another issue raised: this could be a **breaking change** since if multiple matches found, the returning order no longer the last one instead of it could be any one.

For example:
```rust
let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
let num = 1;
let idx = s.binary_search(&num);
s.insert(idx, 2);

// Old implementations
assert_eq!(s, [0, 1, 1, 1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 42, 55]);

// New implementations
assert_eq!(s, [0, 1, 1, 1, 2, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
```

# Benchmarking

**Old implementations**
```sh
$ ./x.py bench --stage 1 library/libcore
test slice::binary_search_l1           ... bench:          59 ns/iter (+/- 4)
test slice::binary_search_l1_with_dups ... bench:          59 ns/iter (+/- 3)
test slice::binary_search_l2           ... bench:          76 ns/iter (+/- 5)
test slice::binary_search_l2_with_dups ... bench:          77 ns/iter (+/- 17)
test slice::binary_search_l3           ... bench:         183 ns/iter (+/- 23)
test slice::binary_search_l3_with_dups ... bench:         185 ns/iter (+/- 19)
```

**New implementations (1)**

Implemented by this PR.
```rust
if cmp == Equal {
    return Ok(mid);
} else if cmp == Less {
    base = mid
}
```
```sh
$ ./x.py bench --stage 1 library/libcore
test slice::binary_search_l1           ... bench:          58 ns/iter (+/- 2)
test slice::binary_search_l1_with_dups ... bench:          37 ns/iter (+/- 4)
test slice::binary_search_l2           ... bench:          76 ns/iter (+/- 3)
test slice::binary_search_l2_with_dups ... bench:          57 ns/iter (+/- 6)
test slice::binary_search_l3           ... bench:         200 ns/iter (+/- 30)
test slice::binary_search_l3_with_dups ... bench:         157 ns/iter (+/- 6)

$ ./x.py bench --stage 1 library/libcore
test slice::binary_search_l1           ... bench:          59 ns/iter (+/- 8)
test slice::binary_search_l1_with_dups ... bench:          37 ns/iter (+/- 2)
test slice::binary_search_l2           ... bench:          77 ns/iter (+/- 2)
test slice::binary_search_l2_with_dups ... bench:          57 ns/iter (+/- 2)
test slice::binary_search_l3           ... bench:         198 ns/iter (+/- 21)
test slice::binary_search_l3_with_dups ... bench:         158 ns/iter (+/- 11)

```

**New implementations (2)**

Suggested by `@nbdd0121` in [comment](https://github.com/rust-lang/rust/pull/74024#issuecomment-665430239).
```rust
base = if cmp == Greater { base } else { mid };
if cmp == Equal { break }
```

```sh
$ ./x.py bench --stage 1 library/libcore
test slice::binary_search_l1           ... bench:          59 ns/iter (+/- 7)
test slice::binary_search_l1_with_dups ... bench:          37 ns/iter (+/- 5)
test slice::binary_search_l2           ... bench:          75 ns/iter (+/- 3)
test slice::binary_search_l2_with_dups ... bench:          56 ns/iter (+/- 3)
test slice::binary_search_l3           ... bench:         195 ns/iter (+/- 15)
test slice::binary_search_l3_with_dups ... bench:         151 ns/iter (+/- 7)

$ ./x.py bench --stage 1 library/libcore
test slice::binary_search_l1           ... bench:          57 ns/iter (+/- 2)
test slice::binary_search_l1_with_dups ... bench:          38 ns/iter (+/- 2)
test slice::binary_search_l2           ... bench:          77 ns/iter (+/- 11)
test slice::binary_search_l2_with_dups ... bench:          57 ns/iter (+/- 4)
test slice::binary_search_l3           ... bench:         194 ns/iter (+/- 15)
test slice::binary_search_l3_with_dups ... bench:         151 ns/iter (+/- 18)

```

I run some benchmarking testings against on two implementations. The new implementation has a lot of improvement in duplicates cases, while in `binary_search_l3` case, it's a little bit slower than the old one.
2021-03-05 20:12:13 +00:00
..
fmt
hash
iter Rollup merge of #82289 - SkiFire13:fix-issue-82282, r=m-ou-se 2021-03-05 10:57:19 +01:00
num Re-enable all num tests on WASM 2021-01-15 16:58:44 -05:00
ops Expand the docs for ops::ControlFlow a bit 2021-02-06 22:36:05 -08:00
alloc.rs
any.rs
array.rs
ascii.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
atomic.rs enable atomic_min/max tests in Miri 2021-03-02 19:58:55 +01:00
bool.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
cell.rs Constantify UnsafeCell::into_inner and related 2020-11-04 11:41:57 +01:00
char.rs
clone.rs Fix borrow and deref 2021-03-03 11:23:29 +01:00
cmp.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
const_ptr.rs Constify ptr::write and the write[_unaligned] methods on *mut T 2021-02-23 18:00:01 +01:00
intrinsics.rs
lazy.rs
lib.rs Constify ptr::write and the write[_unaligned] methods on *mut T 2021-02-23 18:00:01 +01:00
macros.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00
manually_drop.rs
mem.rs Update the bootstrap compiler 2021-02-20 17:19:30 -05:00
nonzero.rs Added impl Rem<NonZeroU{0}> for u{0} which cannot panic 2020-12-17 18:42:19 +02:00
ops.rs Expand the docs for ops::ControlFlow a bit 2021-02-06 22:36:05 -08:00
option.rs Add unwrap_unchecked() methods for Option and Result 2021-01-10 15:14:38 +01:00
pattern.rs
pin.rs
ptr.rs Add a ThinBox library as a libcore test for pointer metadata APIs 2021-02-15 14:27:51 +01:00
result.rs Rename Result::ok_or_err to Result::into_ok_or_err 2021-02-17 08:54:52 -08:00
slice.rs Fix binary_search_by() overflow issue in ZST case 2021-02-27 22:11:44 +08:00
str.rs
str_lossy.rs
task.rs
time.rs
tuple.rs
unicode.rs Make ui test that are run-pass and do not test the compiler itself library tests 2020-11-30 02:47:32 +01:00