Skip to content

Commit

Permalink
slice: #[inline] a couple iterator methods.
Browse files Browse the repository at this point in the history
The one I care about and actually saw in the wild not getting inlined is
clone(). We ended up doing a whole function call for something that just
copies two pointers.

I ended up marking as_slice / as_ref as well because make_slice is
inline(always) itself, and is also the kind of think that can kill
performance in hot loops if you expect it to get inlined. But happy to
undo those.
  • Loading branch information
emilio committed May 4, 2022
1 parent 364bf39 commit 93e587b
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl<'a, T> Iter<'a, T> {
/// ```
#[must_use]
#[stable(feature = "iter_to_slice", since = "1.4.0")]
#[inline]
pub fn as_slice(&self) -> &'a [T] {
self.make_slice()
}
Expand All @@ -146,13 +147,15 @@ iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Clone for Iter<'_, T> {
#[inline]
fn clone(&self) -> Self {
Iter { ptr: self.ptr, end: self.end, _marker: self._marker }
}
}

#[stable(feature = "slice_iter_as_ref", since = "1.13.0")]
impl<T> AsRef<[T]> for Iter<'_, T> {
#[inline]
fn as_ref(&self) -> &[T] {
self.as_slice()
}
Expand Down Expand Up @@ -303,13 +306,15 @@ impl<'a, T> IterMut<'a, T> {
/// ```
#[must_use]
#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
#[inline]
pub fn as_slice(&self) -> &[T] {
self.make_slice()
}
}

#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
impl<T> AsRef<[T]> for IterMut<'_, T> {
#[inline]
fn as_ref(&self) -> &[T] {
self.as_slice()
}
Expand Down

0 comments on commit 93e587b

Please sign in to comment.