Skip to content

Commit faca460

Browse files
authored
Auto merge of #206 - hiddenhare:inline-constructors, r=mbrubeck
Add #[inline] attribute to all fns which return SmallVec When rustc fails to inline a `SmallVec` constructor, it can carry a significant performance cost: for example, if `SmallVec::<[i64; 128]>::from_iter(...)` fails to inline, it will perform an unnecessary 1kb memcpy. I've recently been bitten by this when using `SmallVec` in a context where rustc seemed to be reluctant to perform inlining (a large fn with nested closures). Switching from `SmallVec::from_iter` to `SmallVec::new` and `push`, with a buffer size of 256 bytes, saved over 20ns per call. For larger buffers, `from_iter` carried a proportionally higher cost, even when the actual capacity in use didn't change.
2 parents 9cdf8fb + f5f6d89 commit faca460

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ impl<A: Array> SmallVec<A> {
10551055
/// assert_eq!(&*rebuilt, &[4, 5, 6]);
10561056
/// }
10571057
/// }
1058+
#[inline]
10581059
pub unsafe fn from_raw_parts(ptr: *mut A::Item, length: usize, capacity: usize) -> SmallVec<A> {
10591060
assert!(capacity > A::size());
10601061
SmallVec {
@@ -1372,6 +1373,7 @@ where
13721373
}
13731374

13741375
impl<A: Array> FromIterator<A::Item> for SmallVec<A> {
1376+
#[inline]
13751377
fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> {
13761378
let mut v = SmallVec::new();
13771379
v.extend(iterable);
@@ -1452,6 +1454,7 @@ impl<A: Array> Clone for SmallVec<A>
14521454
where
14531455
A::Item: Clone,
14541456
{
1457+
#[inline]
14551458
fn clone(&self) -> SmallVec<A> {
14561459
let mut new_vector = SmallVec::with_capacity(self.len());
14571460
for element in self.iter() {
@@ -1700,6 +1703,7 @@ trait ToSmallVec<A:Array> {
17001703

17011704
impl<A:Array> ToSmallVec<A> for [A::Item]
17021705
where A::Item: Copy {
1706+
#[inline]
17031707
fn to_smallvec(&self) -> SmallVec<A> {
17041708
SmallVec::from_slice(self)
17051709
}

0 commit comments

Comments
 (0)