Skip to content

Commit 84a7615

Browse files
committed
auto merge of #19574 : erickt/rust/vec-reserve, r=alexcrichton
(I don't understand why this works, and so I don't quite trust this yet. I'm pushing it up to see if anyone else can replicate this performance increase) Somehow llvm is able to optimize this version of Vec::reserve into dramatically faster than the old version. In micro-benchmarks this was 2-10 times faster. It also reduce my Rust compile time from 41 minutes to 27 minutes. Closes #19281.
2 parents 2e996ff + e20ea0b commit 84a7615

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libcollections/vec.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,12 @@ impl<T> Vec<T> {
682682
Some(new_cap) => {
683683
let amort_cap = new_cap.next_power_of_two();
684684
// next_power_of_two will overflow to exactly 0 for really big capacities
685-
if amort_cap == 0 {
686-
self.grow_capacity(new_cap);
685+
let cap = if amort_cap == 0 {
686+
new_cap
687687
} else {
688-
self.grow_capacity(amort_cap);
689-
}
688+
amort_cap
689+
};
690+
self.grow_capacity(cap)
690691
}
691692
}
692693
}

0 commit comments

Comments
 (0)