Skip to content

Commit b1ac082

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 which brings other functions under the threshold. r? `@ghost`
2 parents c30b28b + 69b3155 commit b1ac082

38 files changed

+122
-0
lines changed

library/core/src/array/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ impl fmt::Display for TryFromSliceError {
143143
#[stable(feature = "try_from", since = "1.34.0")]
144144
impl Error for TryFromSliceError {
145145
#[allow(deprecated)]
146+
#[inline]
146147
fn description(&self) -> &str {
147148
"could not convert slice to array"
148149
}
149150
}
150151

151152
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
152153
impl From<Infallible> for TryFromSliceError {
154+
#[inline]
153155
fn from(x: Infallible) -> TryFromSliceError {
154156
match x {}
155157
}

library/core/src/char/convert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ enum CharErrorKind {
190190
#[stable(feature = "char_from_str", since = "1.20.0")]
191191
impl Error for ParseCharError {
192192
#[allow(deprecated)]
193+
#[inline]
193194
fn description(&self) -> &str {
194195
match self.kind {
195196
CharErrorKind::EmptyString => "cannot parse char from empty string",

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

+11
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ impl EscapeDefault {
229229
Self(escape::EscapeIterInner::from_array(data))
230230
}
231231

232+
#[inline]
232233
fn from_unicode(esc: EscapeUnicode) -> Self {
233234
Self(esc.0)
234235
}
@@ -304,6 +305,7 @@ enum EscapeDebugInner {
304305
}
305306

306307
impl EscapeDebug {
308+
#[inline]
307309
fn printable(chr: char) -> Self {
308310
Self(EscapeDebugInner::Char(chr))
309311
}
@@ -314,6 +316,7 @@ impl EscapeDebug {
314316
Self(EscapeDebugInner::Bytes(iter))
315317
}
316318

319+
#[inline]
317320
fn from_unicode(esc: EscapeUnicode) -> Self {
318321
Self(EscapeDebugInner::Bytes(esc.0))
319322
}
@@ -339,6 +342,7 @@ impl Iterator for EscapeDebug {
339342
}
340343
}
341344

345+
#[inline]
342346
fn size_hint(&self) -> (usize, Option<usize>) {
343347
let n = self.len();
344348
(n, Some(n))
@@ -352,6 +356,7 @@ impl Iterator for EscapeDebug {
352356

353357
#[stable(feature = "char_escape_debug", since = "1.20.0")]
354358
impl ExactSizeIterator for EscapeDebug {
359+
#[inline]
355360
fn len(&self) -> usize {
356361
match &self.0 {
357362
EscapeDebugInner::Bytes(bytes) => bytes.len(),
@@ -389,6 +394,7 @@ impl Iterator for ToLowercase {
389394
fn next(&mut self) -> Option<char> {
390395
self.0.next()
391396
}
397+
#[inline]
392398
fn size_hint(&self) -> (usize, Option<usize>) {
393399
self.0.size_hint()
394400
}
@@ -423,6 +429,7 @@ impl Iterator for ToUppercase {
423429
fn next(&mut self) -> Option<char> {
424430
self.0.next()
425431
}
432+
#[inline]
426433
fn size_hint(&self) -> (usize, Option<usize>) {
427434
self.0.size_hint()
428435
}
@@ -450,6 +457,7 @@ enum CaseMappingIter {
450457
}
451458

452459
impl CaseMappingIter {
460+
#[inline]
453461
fn new(chars: [char; 3]) -> CaseMappingIter {
454462
if chars[2] == '\0' {
455463
if chars[1] == '\0' {
@@ -465,6 +473,7 @@ impl CaseMappingIter {
465473

466474
impl Iterator for CaseMappingIter {
467475
type Item = char;
476+
#[inline]
468477
fn next(&mut self) -> Option<char> {
469478
match *self {
470479
CaseMappingIter::Three(a, b, c) => {
@@ -483,6 +492,7 @@ impl Iterator for CaseMappingIter {
483492
}
484493
}
485494

495+
#[inline]
486496
fn size_hint(&self) -> (usize, Option<usize>) {
487497
let size = match self {
488498
CaseMappingIter::Three(..) => 3,
@@ -495,6 +505,7 @@ impl Iterator for CaseMappingIter {
495505
}
496506

497507
impl DoubleEndedIterator for CaseMappingIter {
508+
#[inline]
498509
fn next_back(&mut self) -> Option<char> {
499510
match *self {
500511
CaseMappingIter::Three(a, b, c) => {

library/core/src/convert/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -904,34 +904,39 @@ pub enum Infallible {}
904904

905905
#[stable(feature = "convert_infallible", since = "1.34.0")]
906906
impl Clone for Infallible {
907+
#[inline]
907908
fn clone(&self) -> Infallible {
908909
match *self {}
909910
}
910911
}
911912

912913
#[stable(feature = "convert_infallible", since = "1.34.0")]
913914
impl fmt::Debug for Infallible {
915+
#[inline]
914916
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
915917
match *self {}
916918
}
917919
}
918920

919921
#[stable(feature = "convert_infallible", since = "1.34.0")]
920922
impl fmt::Display for Infallible {
923+
#[inline]
921924
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
922925
match *self {}
923926
}
924927
}
925928

926929
#[stable(feature = "str_parse_error2", since = "1.8.0")]
927930
impl Error for Infallible {
931+
#[inline]
928932
fn description(&self) -> &str {
929933
match *self {}
930934
}
931935
}
932936

933937
#[stable(feature = "convert_infallible", since = "1.34.0")]
934938
impl PartialEq for Infallible {
939+
#[inline]
935940
fn eq(&self, _: &Infallible) -> bool {
936941
match *self {}
937942
}
@@ -942,13 +947,15 @@ impl Eq for Infallible {}
942947

943948
#[stable(feature = "convert_infallible", since = "1.34.0")]
944949
impl PartialOrd for Infallible {
950+
#[inline]
945951
fn partial_cmp(&self, _other: &Self) -> Option<crate::cmp::Ordering> {
946952
match *self {}
947953
}
948954
}
949955

950956
#[stable(feature = "convert_infallible", since = "1.34.0")]
951957
impl Ord for Infallible {
958+
#[inline]
952959
fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
953960
match *self {}
954961
}

library/core/src/error.rs

+5
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ pub struct Request<'a>(dyn Erased<'a> + 'a);
514514

515515
impl<'a> Request<'a> {
516516
/// Create a new `&mut Request` from a `&mut dyn Erased` trait object.
517+
#[inline]
517518
fn new<'b>(erased: &'b mut (dyn Erased<'a> + 'a)) -> &'b mut Request<'a> {
518519
// SAFETY: transmuting `&mut (dyn Erased<'a> + 'a)` to `&mut Request<'a>` is safe since
519520
// `Request` is repr(transparent).
@@ -1046,6 +1047,7 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
10461047
#[stable(feature = "fmt_error", since = "1.11.0")]
10471048
impl Error for crate::fmt::Error {
10481049
#[allow(deprecated)]
1050+
#[inline]
10491051
fn description(&self) -> &str {
10501052
"an error occurred when formatting an argument"
10511053
}
@@ -1054,6 +1056,7 @@ impl Error for crate::fmt::Error {
10541056
#[stable(feature = "try_borrow", since = "1.13.0")]
10551057
impl Error for crate::cell::BorrowError {
10561058
#[allow(deprecated)]
1059+
#[inline]
10571060
fn description(&self) -> &str {
10581061
"already mutably borrowed"
10591062
}
@@ -1062,6 +1065,7 @@ impl Error for crate::cell::BorrowError {
10621065
#[stable(feature = "try_borrow", since = "1.13.0")]
10631066
impl Error for crate::cell::BorrowMutError {
10641067
#[allow(deprecated)]
1068+
#[inline]
10651069
fn description(&self) -> &str {
10661070
"already borrowed"
10671071
}
@@ -1070,6 +1074,7 @@ impl Error for crate::cell::BorrowMutError {
10701074
#[stable(feature = "try_from", since = "1.34.0")]
10711075
impl Error for crate::char::CharTryFromError {
10721076
#[allow(deprecated)]
1077+
#[inline]
10731078
fn description(&self) -> &str {
10741079
"converted integer out of range for `char`"
10751080
}

library/core/src/ffi/c_str.rs

+4
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ enum FromBytesWithNulErrorKind {
124124
}
125125

126126
impl FromBytesWithNulError {
127+
#[inline]
127128
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
128129
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
129130
}
131+
#[inline]
130132
const fn not_nul_terminated() -> FromBytesWithNulError {
131133
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
132134
}
@@ -135,6 +137,7 @@ impl FromBytesWithNulError {
135137
#[stable(feature = "frombyteswithnulerror_impls", since = "1.17.0")]
136138
impl Error for FromBytesWithNulError {
137139
#[allow(deprecated)]
140+
#[inline]
138141
fn description(&self) -> &str {
139142
match self.kind {
140143
FromBytesWithNulErrorKind::InteriorNul(..) => {
@@ -695,6 +698,7 @@ impl AsRef<CStr> for CStr {
695698
/// located within `isize::MAX` from `ptr`.
696699
#[inline]
697700
const unsafe fn const_strlen(ptr: *const c_char) -> usize {
701+
#[inline]
698702
const fn strlen_ct(s: *const c_char) -> usize {
699703
let mut len = 0;
700704

library/core/src/ffi/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ impl<'f> Clone for VaListImpl<'f> {
612612
issue = "44930"
613613
)]
614614
impl<'f> Drop for VaListImpl<'f> {
615+
#[inline]
615616
fn drop(&mut self) {
616617
// FIXME: this should call `va_end`, but there's no clean way to
617618
// guarantee that `drop` always gets inlined into its caller,

library/core/src/fmt/builders.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct PadAdapterState {
1212
}
1313

1414
impl Default for PadAdapterState {
15+
#[inline]
1516
fn default() -> Self {
1617
PadAdapterState { on_newline: true }
1718
}

library/core/src/fmt/float.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ trait GeneralFormat: PartialOrd {
1313
macro_rules! impl_general_format {
1414
($($t:ident)*) => {
1515
$(impl GeneralFormat for $t {
16+
#[inline]
1617
fn already_rounded_value_should_use_exponential(&self) -> bool {
1718
let abs = $t::abs_private(*self);
1819
(abs != 0.0 && abs < 1e-4) || abs >= 1e+16

library/core/src/fmt/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl<'a> Formatter<'a> {
260260
/// Currently not intended for use outside of the standard library.
261261
#[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
262262
#[doc(hidden)]
263+
#[inline]
263264
pub fn new(buf: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
264265
Formatter {
265266
flags: 0,
@@ -1591,6 +1592,7 @@ impl<'a> Formatter<'a> {
15911592
note = "use the `sign_plus`, `sign_minus`, `alternate`, \
15921593
or `sign_aware_zero_pad` methods instead"
15931594
)]
1595+
#[inline]
15941596
pub fn flags(&self) -> u32 {
15951597
self.flags
15961598
}
@@ -1624,6 +1626,7 @@ impl<'a> Formatter<'a> {
16241626
/// ```
16251627
#[must_use]
16261628
#[stable(feature = "fmt_flags", since = "1.5.0")]
1629+
#[inline]
16271630
pub fn fill(&self) -> char {
16281631
self.fill
16291632
}
@@ -1659,6 +1662,7 @@ impl<'a> Formatter<'a> {
16591662
/// ```
16601663
#[must_use]
16611664
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
1665+
#[inline]
16621666
pub fn align(&self) -> Option<Alignment> {
16631667
match self.align {
16641668
rt::Alignment::Left => Some(Alignment::Left),
@@ -1694,6 +1698,7 @@ impl<'a> Formatter<'a> {
16941698
/// ```
16951699
#[must_use]
16961700
#[stable(feature = "fmt_flags", since = "1.5.0")]
1701+
#[inline]
16971702
pub fn width(&self) -> Option<usize> {
16981703
self.width
16991704
}
@@ -1725,6 +1730,7 @@ impl<'a> Formatter<'a> {
17251730
/// ```
17261731
#[must_use]
17271732
#[stable(feature = "fmt_flags", since = "1.5.0")]
1733+
#[inline]
17281734
pub fn precision(&self) -> Option<usize> {
17291735
self.precision
17301736
}
@@ -1757,6 +1763,7 @@ impl<'a> Formatter<'a> {
17571763
/// ```
17581764
#[must_use]
17591765
#[stable(feature = "fmt_flags", since = "1.5.0")]
1766+
#[inline]
17601767
pub fn sign_plus(&self) -> bool {
17611768
self.flags & (1 << rt::Flag::SignPlus as u32) != 0
17621769
}
@@ -1786,6 +1793,7 @@ impl<'a> Formatter<'a> {
17861793
/// ```
17871794
#[must_use]
17881795
#[stable(feature = "fmt_flags", since = "1.5.0")]
1796+
#[inline]
17891797
pub fn sign_minus(&self) -> bool {
17901798
self.flags & (1 << rt::Flag::SignMinus as u32) != 0
17911799
}
@@ -1814,6 +1822,7 @@ impl<'a> Formatter<'a> {
18141822
/// ```
18151823
#[must_use]
18161824
#[stable(feature = "fmt_flags", since = "1.5.0")]
1825+
#[inline]
18171826
pub fn alternate(&self) -> bool {
18181827
self.flags & (1 << rt::Flag::Alternate as u32) != 0
18191828
}
@@ -1840,16 +1849,19 @@ impl<'a> Formatter<'a> {
18401849
/// ```
18411850
#[must_use]
18421851
#[stable(feature = "fmt_flags", since = "1.5.0")]
1852+
#[inline]
18431853
pub fn sign_aware_zero_pad(&self) -> bool {
18441854
self.flags & (1 << rt::Flag::SignAwareZeroPad as u32) != 0
18451855
}
18461856

18471857
// FIXME: Decide what public API we want for these two flags.
18481858
// https://github.com/rust-lang/rust/issues/48584
1859+
#[inline]
18491860
fn debug_lower_hex(&self) -> bool {
18501861
self.flags & (1 << rt::Flag::DebugLowerHex as u32) != 0
18511862
}
18521863

1864+
#[inline]
18531865
fn debug_upper_hex(&self) -> bool {
18541866
self.flags & (1 << rt::Flag::DebugUpperHex as u32) != 0
18551867
}

0 commit comments

Comments
 (0)