Skip to content

Commit

Permalink
Auto merge of rust-lang#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 which brings other functions under the threshold.

r? `@ghost`
  • Loading branch information
bors committed Oct 10, 2023
2 parents c30b28b + 69b3155 commit b1ac082
Show file tree
Hide file tree
Showing 38 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,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 Down
1 change: 1 addition & 0 deletions library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ enum CharErrorKind {
#[stable(feature = "char_from_str", since = "1.20.0")]
impl Error for ParseCharError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
match self.kind {
CharErrorKind::EmptyString => "cannot parse char from empty string",
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
11 changes: 11 additions & 0 deletions library/core/src/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ impl EscapeDefault {
Self(escape::EscapeIterInner::from_array(data))
}

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

impl EscapeDebug {
#[inline]
fn printable(chr: char) -> Self {
Self(EscapeDebugInner::Char(chr))
}
Expand All @@ -314,6 +316,7 @@ impl EscapeDebug {
Self(EscapeDebugInner::Bytes(iter))
}

#[inline]
fn from_unicode(esc: EscapeUnicode) -> Self {
Self(EscapeDebugInner::Bytes(esc.0))
}
Expand All @@ -339,6 +342,7 @@ impl Iterator for EscapeDebug {
}
}

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

#[stable(feature = "char_escape_debug", since = "1.20.0")]
impl ExactSizeIterator for EscapeDebug {
#[inline]
fn len(&self) -> usize {
match &self.0 {
EscapeDebugInner::Bytes(bytes) => bytes.len(),
Expand Down Expand Up @@ -389,6 +394,7 @@ impl Iterator for ToLowercase {
fn next(&mut self) -> Option<char> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
Expand Down Expand Up @@ -423,6 +429,7 @@ impl Iterator for ToUppercase {
fn next(&mut self) -> Option<char> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
Expand Down Expand Up @@ -450,6 +457,7 @@ enum CaseMappingIter {
}

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

impl Iterator for CaseMappingIter {
type Item = char;
#[inline]
fn next(&mut self) -> Option<char> {
match *self {
CaseMappingIter::Three(a, b, c) => {
Expand All @@ -483,6 +492,7 @@ impl Iterator for CaseMappingIter {
}
}

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

impl DoubleEndedIterator for CaseMappingIter {
#[inline]
fn next_back(&mut self) -> Option<char> {
match *self {
CaseMappingIter::Three(a, b, c) => {
Expand Down
7 changes: 7 additions & 0 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,34 +904,39 @@ pub enum Infallible {}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl Clone for Infallible {
#[inline]
fn clone(&self) -> Infallible {
match *self {}
}
}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl fmt::Debug for Infallible {
#[inline]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {}
}
}

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl fmt::Display for Infallible {
#[inline]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {}
}
}

#[stable(feature = "str_parse_error2", since = "1.8.0")]
impl Error for Infallible {
#[inline]
fn description(&self) -> &str {
match *self {}
}
}

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

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

#[stable(feature = "convert_infallible", since = "1.34.0")]
impl Ord for Infallible {
#[inline]
fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
match *self {}
}
Expand Down
5 changes: 5 additions & 0 deletions library/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ pub struct Request<'a>(dyn Erased<'a> + 'a);

impl<'a> Request<'a> {
/// Create a new `&mut Request` from a `&mut dyn Erased` trait object.
#[inline]
fn new<'b>(erased: &'b mut (dyn Erased<'a> + 'a)) -> &'b mut Request<'a> {
// SAFETY: transmuting `&mut (dyn Erased<'a> + 'a)` to `&mut Request<'a>` is safe since
// `Request` is repr(transparent).
Expand Down Expand Up @@ -1046,6 +1047,7 @@ impl<'a, T: Error + ?Sized> Error for &'a T {
#[stable(feature = "fmt_error", since = "1.11.0")]
impl Error for crate::fmt::Error {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"an error occurred when formatting an argument"
}
Expand All @@ -1054,6 +1056,7 @@ impl Error for crate::fmt::Error {
#[stable(feature = "try_borrow", since = "1.13.0")]
impl Error for crate::cell::BorrowError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"already mutably borrowed"
}
Expand All @@ -1062,6 +1065,7 @@ impl Error for crate::cell::BorrowError {
#[stable(feature = "try_borrow", since = "1.13.0")]
impl Error for crate::cell::BorrowMutError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"already borrowed"
}
Expand All @@ -1070,6 +1074,7 @@ impl Error for crate::cell::BorrowMutError {
#[stable(feature = "try_from", since = "1.34.0")]
impl Error for crate::char::CharTryFromError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
"converted integer out of range for `char`"
}
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ enum FromBytesWithNulErrorKind {
}

impl FromBytesWithNulError {
#[inline]
const fn interior_nul(pos: usize) -> FromBytesWithNulError {
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
}
#[inline]
const fn not_nul_terminated() -> FromBytesWithNulError {
FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
}
Expand All @@ -135,6 +137,7 @@ impl FromBytesWithNulError {
#[stable(feature = "frombyteswithnulerror_impls", since = "1.17.0")]
impl Error for FromBytesWithNulError {
#[allow(deprecated)]
#[inline]
fn description(&self) -> &str {
match self.kind {
FromBytesWithNulErrorKind::InteriorNul(..) => {
Expand Down Expand Up @@ -695,6 +698,7 @@ impl AsRef<CStr> for CStr {
/// located within `isize::MAX` from `ptr`.
#[inline]
const unsafe fn const_strlen(ptr: *const c_char) -> usize {
#[inline]
const fn strlen_ct(s: *const c_char) -> usize {
let mut len = 0;

Expand Down
1 change: 1 addition & 0 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ impl<'f> Clone for VaListImpl<'f> {
issue = "44930"
)]
impl<'f> Drop for VaListImpl<'f> {
#[inline]
fn drop(&mut self) {
// FIXME: this should call `va_end`, but there's no clean way to
// guarantee that `drop` always gets inlined into its caller,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct PadAdapterState {
}

impl Default for PadAdapterState {
#[inline]
fn default() -> Self {
PadAdapterState { on_newline: true }
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait GeneralFormat: PartialOrd {
macro_rules! impl_general_format {
($($t:ident)*) => {
$(impl GeneralFormat for $t {
#[inline]
fn already_rounded_value_should_use_exponential(&self) -> bool {
let abs = $t::abs_private(*self);
(abs != 0.0 && abs < 1e-4) || abs >= 1e+16
Expand Down
12 changes: 12 additions & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl<'a> Formatter<'a> {
/// Currently not intended for use outside of the standard library.
#[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
#[doc(hidden)]
#[inline]
pub fn new(buf: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
Formatter {
flags: 0,
Expand Down Expand Up @@ -1591,6 +1592,7 @@ impl<'a> Formatter<'a> {
note = "use the `sign_plus`, `sign_minus`, `alternate`, \
or `sign_aware_zero_pad` methods instead"
)]
#[inline]
pub fn flags(&self) -> u32 {
self.flags
}
Expand Down Expand Up @@ -1624,6 +1626,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn fill(&self) -> char {
self.fill
}
Expand Down Expand Up @@ -1659,6 +1662,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
#[inline]
pub fn align(&self) -> Option<Alignment> {
match self.align {
rt::Alignment::Left => Some(Alignment::Left),
Expand Down Expand Up @@ -1694,6 +1698,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn width(&self) -> Option<usize> {
self.width
}
Expand Down Expand Up @@ -1725,6 +1730,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn precision(&self) -> Option<usize> {
self.precision
}
Expand Down Expand Up @@ -1757,6 +1763,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn sign_plus(&self) -> bool {
self.flags & (1 << rt::Flag::SignPlus as u32) != 0
}
Expand Down Expand Up @@ -1786,6 +1793,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn sign_minus(&self) -> bool {
self.flags & (1 << rt::Flag::SignMinus as u32) != 0
}
Expand Down Expand Up @@ -1814,6 +1822,7 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn alternate(&self) -> bool {
self.flags & (1 << rt::Flag::Alternate as u32) != 0
}
Expand All @@ -1840,16 +1849,19 @@ impl<'a> Formatter<'a> {
/// ```
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
#[inline]
pub fn sign_aware_zero_pad(&self) -> bool {
self.flags & (1 << rt::Flag::SignAwareZeroPad as u32) != 0
}

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

#[inline]
fn debug_upper_hex(&self) -> bool {
self.flags & (1 << rt::Flag::DebugUpperHex as u32) != 0
}
Expand Down
Loading

0 comments on commit b1ac082

Please sign in to comment.