Skip to content

Commit fcd818f

Browse files
committed
Auto merge of rust-lang#116583 - saethlin:inline-small-core-fns, r=<try>
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`
2 parents cdddcd3 + e50157c commit fcd818f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+306
-1
lines changed

library/core/src/alloc/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::ptr::{Alignment, NonNull};
1616
//
1717
// * https://github.com/rust-lang/rust/pull/72189
1818
// * https://github.com/rust-lang/rust/pull/79827
19+
#[inline]
1920
const fn size_align<T>() -> (usize, usize) {
2021
(mem::size_of::<T>(), mem::align_of::<T>())
2122
}

library/core/src/array/iter.rs

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<T, const N: usize> IntoIterator for [T; N] {
5454
/// 2021 edition -- see the [array] Editions section for more information.
5555
///
5656
/// [array]: prim@array
57+
#[inline]
5758
fn into_iter(self) -> Self::IntoIter {
5859
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
5960
// promise:
@@ -77,6 +78,7 @@ impl<T, const N: usize> IntoIter<T, N> {
7778
/// Creates a new iterator over the given `array`.
7879
#[stable(feature = "array_value_iter", since = "1.51.0")]
7980
#[deprecated(since = "1.59.0", note = "use `IntoIterator::into_iter` instead")]
81+
#[inline]
8082
pub fn new(array: [T; N]) -> Self {
8183
IntoIterator::into_iter(array)
8284
}
@@ -139,6 +141,7 @@ impl<T, const N: usize> IntoIter<T, N> {
139141
/// ```
140142
#[unstable(feature = "array_into_iter_constructors", issue = "91583")]
141143
#[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
144+
#[inline]
142145
pub const unsafe fn new_unchecked(
143146
buffer: [MaybeUninit<T>; N],
144147
initialized: Range<usize>,
@@ -357,9 +360,11 @@ impl<T, const N: usize> Drop for IntoIter<T, N> {
357360

358361
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
359362
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
363+
#[inline]
360364
fn len(&self) -> usize {
361365
self.alive.len()
362366
}
367+
#[inline]
363368
fn is_empty(&self) -> bool {
364369
self.alive.is_empty()
365370
}

library/core/src/array/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ where
113113
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
114114
#[stable(feature = "array_from_ref", since = "1.53.0")]
115115
#[rustc_const_stable(feature = "const_array_from_ref_shared", since = "1.63.0")]
116+
#[inline]
116117
pub const fn from_ref<T>(s: &T) -> &[T; 1] {
117118
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
118119
unsafe { &*(s as *const T).cast::<[T; 1]>() }
@@ -121,6 +122,7 @@ pub const fn from_ref<T>(s: &T) -> &[T; 1] {
121122
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
122123
#[stable(feature = "array_from_ref", since = "1.53.0")]
123124
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")]
125+
#[inline]
124126
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
125127
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
126128
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
@@ -143,13 +145,15 @@ impl fmt::Display for TryFromSliceError {
143145
#[stable(feature = "try_from", since = "1.34.0")]
144146
impl Error for TryFromSliceError {
145147
#[allow(deprecated)]
148+
#[inline]
146149
fn description(&self) -> &str {
147150
"could not convert slice to array"
148151
}
149152
}
150153

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

174178
#[stable(feature = "array_borrow", since = "1.4.0")]
175179
impl<T, const N: usize> Borrow<[T]> for [T; N] {
180+
#[inline]
176181
fn borrow(&self) -> &[T] {
177182
self
178183
}
179184
}
180185

181186
#[stable(feature = "array_borrow", since = "1.4.0")]
182187
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
188+
#[inline]
183189
fn borrow_mut(&mut self) -> &mut [T] {
184190
self
185191
}
@@ -321,6 +327,7 @@ impl<'a, T, const N: usize> IntoIterator for &'a [T; N] {
321327
type Item = &'a T;
322328
type IntoIter = Iter<'a, T>;
323329

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

341+
#[inline]
334342
fn into_iter(self) -> IterMut<'a, T> {
335343
self.iter_mut()
336344
}
@@ -435,6 +443,7 @@ macro_rules! array_impl_default {
435443
{$n:expr, $t:ident $($ts:ident)*} => {
436444
#[stable(since = "1.4.0", feature = "array_default")]
437445
impl<T> Default for [T; $n] where T: Default {
446+
#[inline]
438447
fn default() -> [T; $n] {
439448
[$t::default(), $($ts::default()),*]
440449
}
@@ -444,6 +453,7 @@ macro_rules! array_impl_default {
444453
{$n:expr,} => {
445454
#[stable(since = "1.4.0", feature = "array_default")]
446455
impl<T> Default for [T; $n] {
456+
#[inline]
447457
fn default() -> [T; $n] { [] }
448458
}
449459
};
@@ -541,13 +551,15 @@ impl<T, const N: usize> [T; N] {
541551
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
542552
#[stable(feature = "array_as_slice", since = "1.57.0")]
543553
#[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
554+
#[inline]
544555
pub const fn as_slice(&self) -> &[T] {
545556
self
546557
}
547558

548559
/// Returns a mutable slice containing the entire array. Equivalent to
549560
/// `&mut s[..]`.
550561
#[stable(feature = "array_as_slice", since = "1.57.0")]
562+
#[inline]
551563
pub fn as_mut_slice(&mut self) -> &mut [T] {
552564
self
553565
}
@@ -783,7 +795,7 @@ where
783795
R: Try<Output = T>,
784796
R::Residual: Residual<[T; N]>,
785797
{
786-
assert!(iter.size_hint().0 >= N);
798+
#[inline]
787799
fn next<T>(mut iter: impl UncheckedIterator<Item = T>) -> impl FnMut(usize) -> T {
788800
move |_| {
789801
// SAFETY: We know that `from_fn` will call this at most N times,
@@ -792,6 +804,7 @@ where
792804
}
793805
}
794806

807+
assert!(iter.size_hint().0 >= N);
795808
try_from_fn(next(iter))
796809
}
797810

library/core/src/borrow.rs

+5
Original file line numberDiff line numberDiff line change
@@ -207,34 +207,39 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
207207
#[stable(feature = "rust1", since = "1.0.0")]
208208
impl<T: ?Sized> Borrow<T> for T {
209209
#[rustc_diagnostic_item = "noop_method_borrow"]
210+
#[inline]
210211
fn borrow(&self) -> &T {
211212
self
212213
}
213214
}
214215

215216
#[stable(feature = "rust1", since = "1.0.0")]
216217
impl<T: ?Sized> BorrowMut<T> for T {
218+
#[inline]
217219
fn borrow_mut(&mut self) -> &mut T {
218220
self
219221
}
220222
}
221223

222224
#[stable(feature = "rust1", since = "1.0.0")]
223225
impl<T: ?Sized> Borrow<T> for &T {
226+
#[inline]
224227
fn borrow(&self) -> &T {
225228
&**self
226229
}
227230
}
228231

229232
#[stable(feature = "rust1", since = "1.0.0")]
230233
impl<T: ?Sized> Borrow<T> for &mut T {
234+
#[inline]
231235
fn borrow(&self) -> &T {
232236
&**self
233237
}
234238
}
235239

236240
#[stable(feature = "rust1", since = "1.0.0")]
237241
impl<T: ?Sized> BorrowMut<T> for &mut T {
242+
#[inline]
238243
fn borrow_mut(&mut self) -> &mut T {
239244
&mut **self
240245
}

library/core/src/cell.rs

+10
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<T: Ord + Copy> Ord for Cell<T> {
373373
#[stable(feature = "cell_from", since = "1.12.0")]
374374
impl<T> From<T> for Cell<T> {
375375
/// Creates a new `Cell<T>` containing the given value.
376+
#[inline]
376377
fn from(t: T) -> Cell<T> {
377378
Cell::new(t)
378379
}
@@ -488,6 +489,7 @@ impl<T> Cell<T> {
488489
/// ```
489490
#[stable(feature = "move_cell", since = "1.17.0")]
490491
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
492+
#[inline]
491493
pub const fn into_inner(self) -> T {
492494
self.value.into_inner()
493495
}
@@ -658,6 +660,7 @@ impl<T> Cell<[T]> {
658660
/// assert_eq!(slice_cell.len(), 3);
659661
/// ```
660662
#[stable(feature = "as_cell", since = "1.37.0")]
663+
#[inline]
661664
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
662665
// SAFETY: `Cell<T>` has the same memory layout as `T`.
663666
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
@@ -678,6 +681,7 @@ impl<T, const N: usize> Cell<[T; N]> {
678681
/// let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
679682
/// ```
680683
#[unstable(feature = "as_array_of_cells", issue = "88248")]
684+
#[inline]
681685
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
682686
// SAFETY: `Cell<T>` has the same memory layout as `T`.
683687
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
@@ -1172,6 +1176,7 @@ impl<T: ?Sized> RefCell<T> {
11721176
/// assert!(c.try_borrow().is_ok());
11731177
/// ```
11741178
#[unstable(feature = "cell_leak", issue = "69099")]
1179+
#[inline]
11751180
pub fn undo_leak(&mut self) -> &mut T {
11761181
*self.borrow.get_mut() = UNUSED;
11771182
self.get_mut()
@@ -1356,6 +1361,7 @@ impl<T: ?Sized + Ord> Ord for RefCell<T> {
13561361
#[stable(feature = "cell_from", since = "1.12.0")]
13571362
impl<T> From<T> for RefCell<T> {
13581363
/// Creates a new `RefCell<T>` containing the given value.
1364+
#[inline]
13591365
fn from(t: T) -> RefCell<T> {
13601366
RefCell::new(t)
13611367
}
@@ -1577,6 +1583,7 @@ impl<'b, T: ?Sized> Ref<'b, T> {
15771583
/// assert!(cell.try_borrow_mut().is_err());
15781584
/// ```
15791585
#[unstable(feature = "cell_leak", issue = "69099")]
1586+
#[inline]
15801587
pub fn leak(orig: Ref<'b, T>) -> &'b T {
15811588
// By forgetting this Ref we ensure that the borrow counter in the RefCell can't go back to
15821589
// UNUSED within the lifetime `'b`. Resetting the reference tracking state would require a
@@ -1743,6 +1750,7 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
17431750
/// assert!(cell.try_borrow_mut().is_err());
17441751
/// ```
17451752
#[unstable(feature = "cell_leak", issue = "69099")]
1753+
#[inline]
17461754
pub fn leak(mut orig: RefMut<'b, T>) -> &'b mut T {
17471755
// By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell can't
17481756
// go back to UNUSED within the lifetime `'b`. Resetting the reference tracking state would
@@ -2190,6 +2198,7 @@ impl<T: Default> Default for UnsafeCell<T> {
21902198
#[stable(feature = "cell_from", since = "1.12.0")]
21912199
impl<T> From<T> for UnsafeCell<T> {
21922200
/// Creates a new `UnsafeCell<T>` containing the given value.
2201+
#[inline]
21932202
fn from(t: T) -> UnsafeCell<T> {
21942203
UnsafeCell::new(t)
21952204
}
@@ -2290,6 +2299,7 @@ impl<T: Default> Default for SyncUnsafeCell<T> {
22902299
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
22912300
impl<T> From<T> for SyncUnsafeCell<T> {
22922301
/// Creates a new `SyncUnsafeCell<T>` containing the given value.
2302+
#[inline]
22932303
fn from(t: T) -> SyncUnsafeCell<T> {
22942304
SyncUnsafeCell::new(t)
22952305
}

library/core/src/char/decode.rs

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl DecodeUtf16Error {
109109
/// Returns the unpaired surrogate which caused this error.
110110
#[must_use]
111111
#[stable(feature = "decode_utf16", since = "1.9.0")]
112+
#[inline]
112113
pub fn unpaired_surrogate(&self) -> u16 {
113114
self.code
114115
}
@@ -124,6 +125,7 @@ impl fmt::Display for DecodeUtf16Error {
124125
#[stable(feature = "decode_utf16", since = "1.9.0")]
125126
impl Error for DecodeUtf16Error {
126127
#[allow(deprecated)]
128+
#[inline]
127129
fn description(&self) -> &str {
128130
"unpaired surrogate found"
129131
}

library/core/src/char/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,19 @@ impl fmt::Display for EscapeUnicode {
219219
pub struct EscapeDefault(escape::EscapeIterInner<10>);
220220

221221
impl EscapeDefault {
222+
#[inline]
222223
fn printable(chr: ascii::Char) -> Self {
223224
let data = [chr];
224225
Self(escape::EscapeIterInner::from_array(data))
225226
}
226227

228+
#[inline]
227229
fn backslash(chr: ascii::Char) -> Self {
228230
let data = [ascii::Char::ReverseSolidus, chr];
229231
Self(escape::EscapeIterInner::from_array(data))
230232
}
231233

234+
#[inline]
232235
fn from_unicode(esc: EscapeUnicode) -> Self {
233236
Self(esc.0)
234237
}
@@ -304,20 +307,24 @@ enum EscapeDebugInner {
304307
}
305308

306309
impl EscapeDebug {
310+
#[inline]
307311
fn printable(chr: char) -> Self {
308312
Self(EscapeDebugInner::Char(chr))
309313
}
310314

315+
#[inline]
311316
fn backslash(chr: ascii::Char) -> Self {
312317
let data = [ascii::Char::ReverseSolidus, chr];
313318
let iter = escape::EscapeIterInner::from_array(data);
314319
Self(EscapeDebugInner::Bytes(iter))
315320
}
316321

322+
#[inline]
317323
fn from_unicode(esc: EscapeUnicode) -> Self {
318324
Self(EscapeDebugInner::Bytes(esc.0))
319325
}
320326

327+
#[inline]
321328
fn clear(&mut self) {
322329
let bytes = escape::EscapeIterInner::from_array([]);
323330
self.0 = EscapeDebugInner::Bytes(bytes);

0 commit comments

Comments
 (0)