Skip to content

Commit

Permalink
#[inline(always)] on forwarding slice/vec methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed May 12, 2021
1 parent 3773740 commit f4cf82d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
14 changes: 8 additions & 6 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// let vec: Vec<i32> = Vec::with_capacity(10);
/// assert_eq!(vec.capacity(), 10);
/// ```
#[inline]
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize {
self.buf.capacity()
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// let buffer = vec![1, 2, 3, 5, 8];
/// io::sink().write(buffer.as_slice()).unwrap();
/// ```
#[inline]
#[inline(always)]
#[stable(feature = "vec_as_slice", since = "1.7.0")]
pub fn as_slice(&self) -> &[T] {
self
Expand All @@ -1095,7 +1095,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// let mut buffer = vec![0; 3];
/// io::repeat(0b101).read_exact(buffer.as_mut_slice()).unwrap();
/// ```
#[inline]
#[inline(always)]
#[stable(feature = "vec_as_slice", since = "1.7.0")]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self
Expand Down Expand Up @@ -1820,7 +1820,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert_eq!(a.len(), 3);
/// ```
#[doc(alias = "length")]
#[inline]
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.len
Expand Down Expand Up @@ -2422,7 +2422,7 @@ impl<T: Hash, A: Allocator> Hash for Vec<T, A> {
impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {
type Output = I::Output;

#[inline]
#[inline(always)]
fn index(&self, index: I) -> &Self::Output {
Index::index(&**self, index)
}
Expand All @@ -2434,7 +2434,7 @@ impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {
label = "vector indices are of type `usize` or ranges of `usize`"
)]
impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
#[inline]
#[inline(always)]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(&mut **self, index)
}
Expand Down Expand Up @@ -2496,6 +2496,7 @@ impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;

#[inline(always)]
fn into_iter(self) -> slice::Iter<'a, T> {
self.iter()
}
Expand All @@ -2506,6 +2507,7 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;

#[inline(always)]
fn into_iter(self) -> slice::IterMut<'a, T> {
self.iter_mut()
}
Expand Down
28 changes: 15 additions & 13 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub unsafe trait Step: Clone + PartialOrd + Sized {
///
/// * `Step::forward_unchecked(a, n)` is equivalent to `Step::forward(a, n)`
#[unstable(feature = "step_trait_ext", reason = "recently added", issue = "42168")]
#[inline(always)]
unsafe fn forward_unchecked(start: Self, count: usize) -> Self {
Step::forward(start, count)
}
Expand Down Expand Up @@ -179,6 +180,7 @@ pub unsafe trait Step: Clone + PartialOrd + Sized {
///
/// * `Step::backward_unchecked(a, n)` is equivalent to `Step::backward(a, n)`
#[unstable(feature = "step_trait_ext", reason = "recently added", issue = "42168")]
#[inline(always)]
unsafe fn backward_unchecked(start: Self, count: usize) -> Self {
Step::backward(start, count)
}
Expand All @@ -187,13 +189,13 @@ pub unsafe trait Step: Clone + PartialOrd + Sized {
// These are still macro-generated because the integer literals resolve to different types.
macro_rules! step_identical_methods {
() => {
#[inline]
#[inline(always)]
unsafe fn forward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start + n` doesn't overflow.
unsafe { start.unchecked_add(n as Self) }
}

#[inline]
#[inline(always)]
unsafe fn backward_unchecked(start: Self, n: usize) -> Self {
// SAFETY: the caller has to guarantee that `start - n` doesn't overflow.
unsafe { start.unchecked_sub(n as Self) }
Expand Down Expand Up @@ -345,12 +347,12 @@ macro_rules! step_integer_impls {
}
}

#[inline]
#[inline(always)]
fn forward_checked(start: Self, n: usize) -> Option<Self> {
start.checked_add(n as Self)
}

#[inline]
#[inline(always)]
fn backward_checked(start: Self, n: usize) -> Option<Self> {
start.checked_sub(n as Self)
}
Expand All @@ -375,12 +377,12 @@ macro_rules! step_integer_impls {
}
}

#[inline]
#[inline(always)]
fn forward_checked(start: Self, n: usize) -> Option<Self> {
start.checked_add(n as Self)
}

#[inline]
#[inline(always)]
fn backward_checked(start: Self, n: usize) -> Option<Self> {
start.checked_sub(n as Self)
}
Expand Down Expand Up @@ -551,17 +553,17 @@ impl<A: Step> Iterator for ops::Range<A> {
None
}

#[inline]
#[inline(always)]
fn last(mut self) -> Option<A> {
self.next_back()
}

#[inline]
#[inline(always)]
fn min(mut self) -> Option<A> {
self.next()
}

#[inline]
#[inline(always)]
fn max(mut self) -> Option<A> {
self.next_back()
}
Expand Down Expand Up @@ -671,7 +673,7 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
Some(mem::replace(&mut self.start, n))
}

#[inline]
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}
Expand Down Expand Up @@ -793,17 +795,17 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
self.try_fold(init, ok(f)).unwrap()
}

#[inline]
#[inline(always)]
fn last(mut self) -> Option<A> {
self.next_back()
}

#[inline]
#[inline(always)]
fn min(mut self) -> Option<A> {
self.next()
}

#[inline]
#[inline(always)]
fn max(mut self) -> Option<A> {
self.next_back()
}
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl<'a, T> IntoIterator for &'a [T] {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

#[inline(always)]
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
Expand All @@ -31,6 +32,7 @@ impl<'a, T> IntoIterator for &'a mut [T] {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;

#[inline(always)]
fn into_iter(self) -> IterMut<'a, T> {
self.iter_mut()
}
Expand Down
20 changes: 10 additions & 10 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<T> [T] {
/// assert_eq!(None, v.get(0..4));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub fn get<I>(&self, index: I) -> Option<&I::Output>
where
I: SliceIndex<Self>,
Expand All @@ -323,7 +323,7 @@ impl<T> [T] {
/// assert_eq!(x, &[0, 42, 2]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
where
I: SliceIndex<Self>,
Expand Down Expand Up @@ -354,7 +354,7 @@ impl<T> [T] {
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where
I: SliceIndex<Self>,
Expand Down Expand Up @@ -390,7 +390,7 @@ impl<T> [T] {
/// assert_eq!(x, &[1, 13, 4]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where
I: SliceIndex<Self>,
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<T> [T] {
/// [`as_mut_ptr`]: slice::as_mut_ptr
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_slice_as_ptr", since = "1.32.0")]
#[inline]
#[inline(always)]
pub const fn as_ptr(&self) -> *const T {
self as *const [T] as *const T
}
Expand Down Expand Up @@ -457,7 +457,7 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
#[inline]
#[inline(always)]
pub const fn as_mut_ptr(&mut self) -> *mut T {
self as *mut [T] as *mut T
}
Expand Down Expand Up @@ -702,7 +702,7 @@ impl<T> [T] {
/// assert_eq!(iterator.next(), None);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub fn iter(&self) -> Iter<'_, T> {
Iter::new(self)
}
Expand All @@ -719,7 +719,7 @@ impl<T> [T] {
/// assert_eq!(x, &[3, 4, 6]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[inline(always)]
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut::new(self)
}
Expand Down Expand Up @@ -3544,7 +3544,7 @@ pub trait SlicePattern {
impl<T> SlicePattern for [T] {
type Item = T;

#[inline]
#[inline(always)]
fn as_slice(&self) -> &[Self::Item] {
self
}
Expand All @@ -3554,7 +3554,7 @@ impl<T> SlicePattern for [T] {
impl<T, const N: usize> SlicePattern for [T; N] {
type Item = T;

#[inline]
#[inline(always)]
fn as_slice(&self) -> &[Self::Item] {
self
}
Expand Down

0 comments on commit f4cf82d

Please sign in to comment.