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

Optimize slice.{r}position result bounds check #45501

Closed
Closed
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
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ matrix:
- env: IMAGE=dist-armv7-linux DEPLOY=1
if: branch = auto
- env: IMAGE=dist-i586-gnu-i686-musl DEPLOY=1
if: branch = auto
- env: IMAGE=dist-i686-freebsd DEPLOY=1
if: branch = auto
- env: IMAGE=dist-i686-linux DEPLOY=1
if: branch = auto
- env: IMAGE=dist-mips-linux DEPLOY=1
if: branch = auto
- env: IMAGE=dist-mips64-linux DEPLOY=1
Expand All @@ -152,7 +150,6 @@ matrix:
- env: IMAGE=dist-x86_64-freebsd DEPLOY=1
if: branch = auto
- env: IMAGE=dist-x86_64-musl DEPLOY=1
if: branch = auto
- env: IMAGE=dist-x86_64-netbsd DEPLOY=1
if: branch = auto
- env: IMAGE=asmjs
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,10 @@ macro_rules! iterator {
fn position<F>(&mut self, mut predicate: F) -> Option<usize>
where F: FnMut(Self::Item) -> bool,
{
let len = self.len();
let mut index = 0;
self.search_while(None, move |elt| {
unsafe { assume(index < len); }
if predicate(elt) {
SearchWhile::Done(Some(index))
} else {
Expand All @@ -1213,9 +1215,11 @@ macro_rules! iterator {
fn rposition<F>(&mut self, mut predicate: F) -> Option<usize>
where F: FnMut(Self::Item) -> bool,
{
let len = self.len();
let mut index = self.len();
self.rsearch_while(None, move |elt| {
index -= 1;
unsafe { assume(index < len); }
if predicate(elt) {
SearchWhile::Done(Some(index))
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/libcore/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ use core::slice::heapsort;
use core::result::Result::{Ok, Err};
use rand::{Rng, XorShiftRng};


#[test]
fn test_position() {
let b = [1, 2, 3, 5, 5];
assert!(b.iter().position(|&v| v == 9) == None);
assert!(b.iter().position(|&v| v == 5) == Some(3));
assert!(b.iter().position(|&v| v == 3) == Some(2));
assert!(b.iter().position(|&v| v == 0) == None);
}

#[test]
fn test_rposition() {
let b = [1, 2, 3, 5, 5];
assert!(b.iter().rposition(|&v| v == 9) == None);
assert!(b.iter().rposition(|&v| v == 5) == Some(4));
assert!(b.iter().rposition(|&v| v == 3) == Some(2));
assert!(b.iter().rposition(|&v| v == 0) == None);
}

#[test]
fn test_binary_search() {
let b = [1, 2, 4, 6, 8, 9];
Expand Down