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

Eliminate bounds checking in slice::Windows #77617

Merged
merged 2 commits into from
Oct 7, 2020
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
29 changes: 15 additions & 14 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::intrinsics::{assume, exact_div, unchecked_sub};
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
use crate::marker::{PhantomData, Send, Sized, Sync};
use crate::mem;
use crate::num::NonZeroUsize;
use crate::ptr::NonNull;

use super::{from_raw_parts, from_raw_parts_mut};
Expand Down Expand Up @@ -1187,12 +1188,12 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Windows<'a, T: 'a> {
v: &'a [T],
size: usize,
size: NonZeroUsize,
}

impl<'a, T: 'a> Windows<'a, T> {
#[inline]
pub(super) fn new(slice: &'a [T], size: usize) -> Self {
pub(super) fn new(slice: &'a [T], size: NonZeroUsize) -> Self {
Self { v: slice, size }
}
}
Expand All @@ -1211,21 +1212,21 @@ impl<'a, T> Iterator for Windows<'a, T> {

#[inline]
fn next(&mut self) -> Option<&'a [T]> {
if self.size > self.v.len() {
if self.size.get() > self.v.len() {
None
} else {
let ret = Some(&self.v[..self.size]);
let ret = Some(&self.v[..self.size.get()]);
self.v = &self.v[1..];
ret
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.size > self.v.len() {
if self.size.get() > self.v.len() {
(0, Some(0))
} else {
let size = self.v.len() - self.size + 1;
let size = self.v.len() - self.size.get() + 1;
(size, Some(size))
}
}
Expand All @@ -1237,7 +1238,7 @@ impl<'a, T> Iterator for Windows<'a, T> {

#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
let (end, overflow) = self.size.overflowing_add(n);
let (end, overflow) = self.size.get().overflowing_add(n);
if end > self.v.len() || overflow {
self.v = &[];
None
Expand All @@ -1250,10 +1251,10 @@ impl<'a, T> Iterator for Windows<'a, T> {

#[inline]
fn last(self) -> Option<Self::Item> {
if self.size > self.v.len() {
if self.size.get() > self.v.len() {
None
} else {
let start = self.v.len() - self.size;
let start = self.v.len() - self.size.get();
Some(&self.v[start..])
}
}
Expand All @@ -1264,18 +1265,18 @@ impl<'a, T> Iterator for Windows<'a, T> {
// which means that `i` cannot overflow an `isize`, and the
// slice created by `from_raw_parts` is a subslice of `self.v`
// thus is guaranteed to be valid for the lifetime `'a` of `self.v`.
unsafe { from_raw_parts(self.v.as_ptr().add(idx), self.size) }
unsafe { from_raw_parts(self.v.as_ptr().add(idx), self.size.get()) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for Windows<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
if self.size > self.v.len() {
if self.size.get() > self.v.len() {
None
} else {
let ret = Some(&self.v[self.v.len() - self.size..]);
let ret = Some(&self.v[self.v.len() - self.size.get()..]);
self.v = &self.v[..self.v.len() - 1];
ret
}
Expand All @@ -1284,11 +1285,11 @@ impl<'a, T> DoubleEndedIterator for Windows<'a, T> {
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
let (end, overflow) = self.v.len().overflowing_sub(n);
if end < self.size || overflow {
if end < self.size.get() || overflow {
self.v = &[];
None
} else {
let ret = &self.v[end - self.size..end];
let ret = &self.v[end - self.size.get()..end];
self.v = &self.v[..end - 1];
Some(ret)
}
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::marker::Copy;
use crate::mem;
use crate::num::NonZeroUsize;
use crate::ops::{FnMut, Range, RangeBounds};
use crate::option::Option;
use crate::option::Option::{None, Some};
Expand Down Expand Up @@ -751,7 +752,7 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn windows(&self, size: usize) -> Windows<'_, T> {
assert_ne!(size, 0);
let size = NonZeroUsize::new(size).expect("size is zero");
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have guideline about what the phrase used in expect call ?

Suggested change
let size = NonZeroUsize::new(size).expect("size is zero");
let size = NonZeroUsize::new(size).expect("`size` cannot be zero");

Copy link
Contributor Author

@AnthonyMikh AnthonyMikh Oct 7, 2020

Choose a reason for hiding this comment

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

I am not sure, but before my change panic message was assertion failed: size != 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is bike-shredding so don't worry much about this.

Windows::new(self, size)
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/codegen/slice-windows-no-bounds-check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![crate_type = "lib"]

// compile-flags: -O

use std::slice::Windows;

// CHECK-LABEL: @naive_string_search
#[no_mangle]
pub fn naive_string_search(haystack: &str, needle: &str) -> Option<usize> {
if needle.is_empty() {
return Some(0);
}
// CHECK-NOT: panic
// CHECK-NOT: fail
haystack
.as_bytes()
.windows(needle.len())
.position(|sub| sub == needle.as_bytes())
}

// CHECK-LABEL: @next
#[no_mangle]
pub fn next<'a>(w: &mut Windows<'a, u32>) -> Option<&'a [u32]> {
// CHECK-NOT: panic
// CHECK-NOT: fail
w.next()
}

// CHECK-LABEL: @next_back
#[no_mangle]
pub fn next_back<'a>(w: &mut Windows<'a, u32>) -> Option<&'a [u32]> {
// CHECK-NOT: panic
// CHECK-NOT: fail
w.next_back()
}