Skip to content

Commit 360f2f0

Browse files
committed
Inline most of the code paths for conversions with boxed slices
This helps with the specific problem described in #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.
1 parent 8dd24c8 commit 360f2f0

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

src/liballoc/boxed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,15 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
577577

578578
#[stable(feature = "box_from_slice", since = "1.17.0")]
579579
impl<'a> From<&'a str> for Box<str> {
580+
#[inline]
580581
fn from(s: &'a str) -> Box<str> {
581582
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
582583
}
583584
}
584585

585586
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
586587
impl From<Box<str>> for Box<[u8]> {
588+
#[inline]
587589
fn from(s: Box<str>) -> Self {
588590
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
589591
}

src/liballoc/str.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,7 @@ impl str {
18111811
/// assert_eq!(*boxed_bytes, *s.as_bytes());
18121812
/// ```
18131813
#[stable(feature = "str_box_extras", since = "1.20.0")]
1814+
#[inline]
18141815
pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> {
18151816
self.into()
18161817
}
@@ -2049,6 +2050,7 @@ impl str {
20492050
/// assert_eq!(boxed_str.into_string(), string);
20502051
/// ```
20512052
#[stable(feature = "box_str", since = "1.4.0")]
2053+
#[inline]
20522054
pub fn into_string(self: Box<str>) -> String {
20532055
let slice = Box::<[u8]>::from(self);
20542056
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
@@ -2307,6 +2309,7 @@ impl str {
23072309
/// assert_eq!("☺", &*smile);
23082310
/// ```
23092311
#[stable(feature = "str_box_extras", since = "1.20.0")]
2312+
#[inline]
23102313
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
23112314
Box::from_raw(Box::into_raw(v) as *mut str)
23122315
}

src/liballoc/string.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ impl String {
15871587
/// let b = s.into_boxed_str();
15881588
/// ```
15891589
#[stable(feature = "box_str", since = "1.4.0")]
1590+
#[inline]
15901591
pub fn into_boxed_str(self) -> Box<str> {
15911592
let slice = self.vec.into_boxed_slice();
15921593
unsafe { from_boxed_utf8_unchecked(slice) }

src/liballoc/vec.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,11 @@ impl<T> Vec<T> {
582582
/// assert!(vec.capacity() >= 3);
583583
/// ```
584584
#[stable(feature = "rust1", since = "1.0.0")]
585+
#[inline]
585586
pub fn shrink_to_fit(&mut self) {
586-
self.buf.shrink_to_fit(self.len);
587+
if self.capacity() != self.len {
588+
self.buf.shrink_to_fit(self.len);
589+
}
587590
}
588591

589592
/// Shrinks the capacity of the vector with a lower bound.
@@ -636,6 +639,7 @@ impl<T> Vec<T> {
636639
/// assert_eq!(slice.into_vec().capacity(), 3);
637640
/// ```
638641
#[stable(feature = "rust1", since = "1.0.0")]
642+
#[inline(always)]
639643
pub fn into_boxed_slice(mut self) -> Box<[T]> {
640644
unsafe {
641645
self.shrink_to_fit();

0 commit comments

Comments
 (0)