]> git.lizzy.rs Git - rust.git/commitdiff
collections: dramatically speed up Vec::reserve with magic
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Fri, 5 Dec 2014 19:29:15 +0000 (11:29 -0800)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Fri, 5 Dec 2014 19:29:41 +0000 (11:29 -0800)
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.

src/libcollections/vec.rs

index 2396cf8cec67ce20d235031f3a44e8888728b2c7..34d9397742dda8d6b049e1a3ed8543e21581c16e 100644 (file)
@@ -688,11 +688,12 @@ pub fn reserve(&mut self, additional: uint) {
                 Some(new_cap) => {
                     let amort_cap = new_cap.next_power_of_two();
                     // next_power_of_two will overflow to exactly 0 for really big capacities
-                    if amort_cap == 0 {
-                        self.grow_capacity(new_cap);
+                    let cap = if amort_cap == 0 {
+                        new_cap
                     } else {
-                        self.grow_capacity(amort_cap);
-                    }
+                        amort_cap
+                    };
+                    self.grow_capacity(cap)
                 }
             }
         }