Skip to content

Commit e20ea0b

Browse files
committed
collections: dramatically speed up Vec::reserve with magic
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 shaved 14 minutes off of rust's compile times. Closes #19281.
1 parent 4573da6 commit e20ea0b

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
@@ -688,11 +688,12 @@ impl<T> Vec<T> {
688688
Some(new_cap) => {
689689
let amort_cap = new_cap.next_power_of_two();
690690
// next_power_of_two will overflow to exactly 0 for really big capacities
691-
if amort_cap == 0 {
692-
self.grow_capacity(new_cap);
691+
let cap = if amort_cap == 0 {
692+
new_cap
693693
} else {
694-
self.grow_capacity(amort_cap);
695-
}
694+
amort_cap
695+
};
696+
self.grow_capacity(cap)
696697
}
697698
}
698699
}

0 commit comments

Comments
 (0)