Skip to content

Commit

Permalink
Auto merge of #116583 - saethlin:inline-small-core-fns, r=<try>
Browse files Browse the repository at this point in the history
Add #[inline] to small functions in core

Where "small" is strictly defined as optimized_mir with 5 or less statements and no calls. I've also applied that heuristic recursively; applying it once causes some functions to become eligible for MIR inlining bring other functions under the threshold.

r? `@ghost`
  • Loading branch information
bors committed Oct 9, 2023
2 parents cdddcd3 + e50157c commit fcd818f
Show file tree
Hide file tree
Showing 74 changed files with 306 additions and 1 deletion.
1 change: 1 addition & 0 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::ptr::{Alignment, NonNull};
//
// * https://github.com/rust-lang/rust/pull/72189
// * https://github.com/rust-lang/rust/pull/79827
#[inline]
const fn size_align<T>() -> (usize, usize) {
(mem::size_of::<T>(), mem::align_of::<T>())
}
Expand Down
5 changes: 5 additions & 0 deletions library/core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<T, const N: usize> IntoIterator for [T; N] {
/// 2021 edition -- see the [array] Editions section for more information.
///
/// [array]: prim@array
#[inline]
fn into_iter(self) -> Self::IntoIter {
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
// promise:
Expand All @@ -77,6 +78,7 @@ impl<T, const N: usize> IntoIter<T, N> {
/// Creates a new iterator over the given `array`.
#[stable(feature = "array_value_iter", since = "1.51.0")]
#[deprecated(since = "1.59.0", note = "use `IntoIterator::into_iter` instead")]
#[inline]
pub fn new(array: [T; N]) -> Self {
IntoIterator::into_iter(array)
}
Expand Down Expand Up @@ -139,6 +141,7 @@ impl<T, const N: usize> IntoIter<T, N> {
/// ```
#[unstable(feature = "array_into_iter_constructors", issue = "91583")]
#[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
#[inline]
pub const unsafe fn new_unchecked(
buffer: [MaybeUninit<T>; N],
initialized: Range<usize>,
Expand Down Expand Up @@ -357,9 +360,11 @@ impl<T, const N: usize> Drop for IntoIter<T, N> {

#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
#[inline]
fn len(&self) -> usize {
self.alive.len()
}
#[inline]
fn is_empty(&self) -> bool {
self.alive.is_empty()
}
Expand Down
15 changes: 14 additions & 1 deletion library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ where
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
#[stable(feature = "array_from_ref", since = "1.53.0")]
#[rustc_const_stable(feature = "const_array_from_ref_shared", since = "1.63.0")]
#[inline]
pub const fn from_ref<T>(s: &T) -> &[T; 1] {
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
unsafe { &*(s as *const T).cast::<[T; 1]>() }
Expand All @@ -121,6 +122,7 @@ pub const fn from_ref<T>(s: &T) -> &[T; 1] {
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
#[stable(feature = "array_from_ref", since = "1.53.0")]
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")]
#[inline]
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
Expand All @@ -143,13 +145,15 @@ impl fmt::Display for TryFromSliceError {
#[stable(feature = "try_from", since = "1.34.0")]
impl Error for TryFromSliceError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"could not convert slice to array"
}
}

#[stable(feature = "try_from_slice_error", since = "1.36.0")]
impl From<Infallible> for TryFromSliceError {
#[inline]
fn from(x: Infallible) -> TryFromSliceError {
match x {}
}
Expand All @@ -173,13 +177,15 @@ impl<T, const N: usize> AsMut<[T]> for [T; N] {

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> Borrow<[T]> for [T; N] {
#[inline]
fn borrow(&self) -> &[T] {
self
}
}

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
#[inline]
fn borrow_mut(&mut self) -> &mut [T] {
self
}
Expand Down Expand Up @@ -321,6 +327,7 @@ impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

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

#[inline]
fn into_iter(self) -> IterMut<'a, T> {
self.iter_mut()
}
Expand Down Expand Up @@ -435,6 +443,7 @@ macro_rules! array_impl_default {
{$n:expr, $t:ident $($ts:ident)*} => {
#[stable(since = "1.4.0", feature = "array_default")]
impl<T> Default for [T; $n] where T: Default {
#[inline]
fn default() -> [T; $n] {
[$t::default(), $($ts::default()),*]
}
Expand All @@ -444,6 +453,7 @@ macro_rules! array_impl_default {
{$n:expr,} => {
#[stable(since = "1.4.0", feature = "array_default")]
impl<T> Default for [T; $n] {
#[inline]
fn default() -> [T; $n] { [] }
}
};
Expand Down Expand Up @@ -541,13 +551,15 @@ impl<T, const N: usize> [T; N] {
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
#[stable(feature = "array_as_slice", since = "1.57.0")]
#[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
#[inline]
pub const fn as_slice(&self) -> &[T] {
self
}

/// Returns a mutable slice containing the entire array. Equivalent to
/// `&mut s[..]`.
#[stable(feature = "array_as_slice", since = "1.57.0")]
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self
}
Expand Down Expand Up @@ -783,7 +795,7 @@ where
R: Try<Output = T>,
R::Residual: Residual<[T; N]>,
{
assert!(iter.size_hint().0 >= N);
#[inline]
fn next<T>(mut iter: impl UncheckedIterator<Item = T>) -> impl FnMut(usize) -> T {
move |_| {
// SAFETY: We know that `from_fn` will call this at most N times,
Expand All @@ -792,6 +804,7 @@ where
}
}

assert!(iter.size_hint().0 >= N);
try_from_fn(next(iter))
}

Expand Down
5 changes: 5 additions & 0 deletions library/core/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,34 +207,39 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for T {
#[rustc_diagnostic_item = "noop_method_borrow"]
#[inline]
fn borrow(&self) -> &T {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> BorrowMut<T> for T {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for &T {
#[inline]
fn borrow(&self) -> &T {
&**self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for &mut T {
#[inline]
fn borrow(&self) -> &T {
&**self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> BorrowMut<T> for &mut T {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
&mut **self
}
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ impl<T: Ord + Copy> Ord for Cell<T> {
#[stable(feature = "cell_from", since = "1.12.0")]
impl<T> From<T> for Cell<T> {
/// Creates a new `Cell<T>` containing the given value.
#[inline]
fn from(t: T) -> Cell<T> {
Cell::new(t)
}
Expand Down Expand Up @@ -488,6 +489,7 @@ impl<T> Cell<T> {
/// ```
#[stable(feature = "move_cell", since = "1.17.0")]
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[inline]
pub const fn into_inner(self) -> T {
self.value.into_inner()
}
Expand Down Expand Up @@ -658,6 +660,7 @@ impl<T> Cell<[T]> {
/// assert_eq!(slice_cell.len(), 3);
/// ```
#[stable(feature = "as_cell", since = "1.37.0")]
#[inline]
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
// SAFETY: `Cell<T>` has the same memory layout as `T`.
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
Expand All @@ -678,6 +681,7 @@ impl<T, const N: usize> Cell<[T; N]> {
/// let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
/// ```
#[unstable(feature = "as_array_of_cells", issue = "88248")]
#[inline]
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
// SAFETY: `Cell<T>` has the same memory layout as `T`.
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
Expand Down Expand Up @@ -1172,6 +1176,7 @@ impl<T: ?Sized> RefCell<T> {
/// assert!(c.try_borrow().is_ok());
/// ```
#[unstable(feature = "cell_leak", issue = "69099")]
#[inline]
pub fn undo_leak(&mut self) -> &mut T {
*self.borrow.get_mut() = UNUSED;
self.get_mut()
Expand Down Expand Up @@ -1356,6 +1361,7 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
#[stable(feature = "cell_from", since = "1.12.0")]
impl<T> From<T> for RefCell<T> {
/// Creates a new `RefCell<T>` containing the given value.
#[inline]
fn from(t: T) -> RefCell<T> {
RefCell::new(t)
}
Expand Down Expand Up @@ -1577,6 +1583,7 @@ impl<'b, T: ?Sized> Ref<'b, T> {
/// assert!(cell.try_borrow_mut().is_err());
/// ```
#[unstable(feature = "cell_leak", issue = "69099")]
#[inline]
pub fn leak(orig: Ref<'b, T>) -> &'b T {
// By forgetting this Ref we ensure that the borrow counter in the RefCell can't go back to
// UNUSED within the lifetime `'b`. Resetting the reference tracking state would require a
Expand Down Expand Up @@ -1743,6 +1750,7 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
/// assert!(cell.try_borrow_mut().is_err());
/// ```
#[unstable(feature = "cell_leak", issue = "69099")]
#[inline]
pub fn leak(mut orig: RefMut<'b, T>) -> &'b mut T {
// By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell can't
// go back to UNUSED within the lifetime `'b`. Resetting the reference tracking state would
Expand Down Expand Up @@ -2190,6 +2198,7 @@ impl<T: Default> Default for UnsafeCell<T> {
#[stable(feature = "cell_from", since = "1.12.0")]
impl<T> From<T> for UnsafeCell<T> {
/// Creates a new `UnsafeCell<T>` containing the given value.
#[inline]
fn from(t: T) -> UnsafeCell<T> {
UnsafeCell::new(t)
}
Expand Down Expand Up @@ -2290,6 +2299,7 @@ impl<T: Default> Default for SyncUnsafeCell<T> {
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
impl<T> From<T> for SyncUnsafeCell<T> {
/// Creates a new `SyncUnsafeCell<T>` containing the given value.
#[inline]
fn from(t: T) -> SyncUnsafeCell<T> {
SyncUnsafeCell::new(t)
}
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/char/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl DecodeUtf16Error {
/// Returns the unpaired surrogate which caused this error.
#[must_use]
#[stable(feature = "decode_utf16", since = "1.9.0")]
#[inline]
pub fn unpaired_surrogate(&self) -> u16 {
self.code
}
Expand All @@ -124,6 +125,7 @@ impl fmt::Display for DecodeUtf16Error {
#[stable(feature = "decode_utf16", since = "1.9.0")]
impl Error for DecodeUtf16Error {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"unpaired surrogate found"
}
Expand Down
7 changes: 7 additions & 0 deletions library/core/src/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,19 @@ impl fmt::Display for EscapeUnicode {
pub struct EscapeDefault(escape::EscapeIterInner<10>);

impl EscapeDefault {
#[inline]
fn printable(chr: ascii::Char) -> Self {
let data = [chr];
Self(escape::EscapeIterInner::from_array(data))
}

#[inline]
fn backslash(chr: ascii::Char) -> Self {
let data = [ascii::Char::ReverseSolidus, chr];
Self(escape::EscapeIterInner::from_array(data))
}

#[inline]
fn from_unicode(esc: EscapeUnicode) -> Self {
Self(esc.0)
}
Expand Down Expand Up @@ -304,20 +307,24 @@ enum EscapeDebugInner {
}

impl EscapeDebug {
#[inline]
fn printable(chr: char) -> Self {
Self(EscapeDebugInner::Char(chr))
}

#[inline]
fn backslash(chr: ascii::Char) -> Self {
let data = [ascii::Char::ReverseSolidus, chr];
let iter = escape::EscapeIterInner::from_array(data);
Self(EscapeDebugInner::Bytes(iter))
}

#[inline]
fn from_unicode(esc: EscapeUnicode) -> Self {
Self(EscapeDebugInner::Bytes(esc.0))
}

#[inline]
fn clear(&mut self) {
let bytes = escape::EscapeIterInner::from_array([]);
self.0 = EscapeDebugInner::Bytes(bytes);
Expand Down
Loading

0 comments on commit fcd818f

Please sign in to comment.