Skip to content

Commit bf60295

Browse files
authored
Rollup merge of rust-lang#49555 - nox:inline-into-boxed, r=alexcrichton
Inline most of the code paths for conversions with boxed slices This helps with the specific problem described in rust-lang#49541, obviously without making any large change to how inlining works in the general case. Everything involved in the conversions is made `#[inline]`, except for the `<Vec<T>>::into_boxed_slice` entry point which is made `#[inline(always)]` after checking that duplicating the function mentioned in the issue prevented its inlining if I only annotate it with `#[inline]`. For the record, that function was: ```rust pub fn foo() -> Box<[u8]> { vec![0].into_boxed_slice() } ``` To help the inliner's job, we also hoist a `self.capacity() != self.len` check in `<Vec<T>>::shrink_to_fit` and mark it as `#[inline]` too.
2 parents 1ef1563 + b59fa0d commit bf60295

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

src/liballoc/boxed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
429429

430430
#[stable(feature = "box_from_slice", since = "1.17.0")]
431431
impl<'a> From<&'a str> for Box<str> {
432+
#[inline]
432433
fn from(s: &'a str) -> Box<str> {
433434
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
434435
}
435436
}
436437

437438
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
438439
impl From<Box<str>> for Box<[u8]> {
440+
#[inline]
439441
fn from(s: Box<str>) -> Self {
440442
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
441443
}

src/liballoc/str.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,7 @@ impl str {
18271827
/// assert_eq!(*boxed_bytes, *s.as_bytes());
18281828
/// ```
18291829
#[stable(feature = "str_box_extras", since = "1.20.0")]
1830+
#[inline]
18301831
pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> {
18311832
self.into()
18321833
}
@@ -2065,6 +2066,7 @@ impl str {
20652066
/// assert_eq!(boxed_str.into_string(), string);
20662067
/// ```
20672068
#[stable(feature = "box_str", since = "1.4.0")]
2069+
#[inline]
20682070
pub fn into_string(self: Box<str>) -> String {
20692071
let slice = Box::<[u8]>::from(self);
20702072
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
@@ -2323,6 +2325,7 @@ impl str {
23232325
/// assert_eq!("☺", &*smile);
23242326
/// ```
23252327
#[stable(feature = "str_box_extras", since = "1.20.0")]
2328+
#[inline]
23262329
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
23272330
Box::from_raw(Box::into_raw(v) as *mut str)
23282331
}

src/liballoc/string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,7 @@ impl String {
15861586
/// let b = s.into_boxed_str();
15871587
/// ```
15881588
#[stable(feature = "box_str", since = "1.4.0")]
1589+
#[inline]
15891590
pub fn into_boxed_str(self) -> Box<str> {
15901591
let slice = self.vec.into_boxed_slice();
15911592
unsafe { from_boxed_utf8_unchecked(slice) }

src/liballoc/vec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ impl<T> Vec<T> {
583583
/// ```
584584
#[stable(feature = "rust1", since = "1.0.0")]
585585
pub fn shrink_to_fit(&mut self) {
586-
self.buf.shrink_to_fit(self.len);
586+
if self.capacity() != self.len {
587+
self.buf.shrink_to_fit(self.len);
588+
}
587589
}
588590

589591
/// Shrinks the capacity of the vector with a lower bound.

0 commit comments

Comments
 (0)