Skip to content

Commit c3e8aa3

Browse files
authored
Auto merge of #241 - mpdn:master, r=mbrubeck
Remove extraneous branch from push `push` does two branches on the "smallness" of the `smallvec`: one before the reserve check and one after. LLVM doesn't seem to optimize the second branch away for the (very common) non-growing case. In addition, in the growing branch we know the memory will be on the heap, so no need to branch here. On my machine, this improves `bench_push` from approx 300ns to 263ns (+/- 5 on both).
2 parents fc12fb1 + 79f4132 commit c3e8aa3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,13 +753,15 @@ impl<A: Array> SmallVec<A> {
753753
#[inline]
754754
pub fn push(&mut self, value: A::Item) {
755755
unsafe {
756-
let (_, &mut len, cap) = self.triple_mut();
757-
if len == cap {
756+
let (mut ptr, mut len, cap) = self.triple_mut();
757+
if *len == cap {
758758
self.reserve(1);
759+
let &mut (heap_ptr, ref mut heap_len) = self.data.heap_mut();
760+
ptr = heap_ptr;
761+
len = heap_len;
759762
}
760-
let (ptr, len_ptr, _) = self.triple_mut();
761-
*len_ptr = len + 1;
762-
ptr::write(ptr.add(len), value);
763+
ptr::write(ptr.add(*len), value);
764+
*len += 1;
763765
}
764766
}
765767

0 commit comments

Comments
 (0)