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.
This commit is contained in:
Anthony Ramine
2018-03-31 23:19:02 +02:00
parent 8dd24c8ed4
commit 360f2f036d
4 changed files with 11 additions and 1 deletions

View File

@@ -582,8 +582,11 @@ impl<T> Vec<T> {
/// assert!(vec.capacity() >= 3);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn shrink_to_fit(&mut self) {
self.buf.shrink_to_fit(self.len);
if self.capacity() != self.len {
self.buf.shrink_to_fit(self.len);
}
}
/// Shrinks the capacity of the vector with a lower bound.
@@ -636,6 +639,7 @@ impl<T> Vec<T> {
/// assert_eq!(slice.into_vec().capacity(), 3);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)]
pub fn into_boxed_slice(mut self) -> Box<[T]> {
unsafe {
self.shrink_to_fit();