From c384ee18fcb55274682e8a9a24608bfc825bedce Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Mon, 19 Jan 2015 10:15:17 -0800 Subject: [PATCH] Don't reallocate when capacity is already equal to length `Vec::shrink_to_fit()` may be called on vectors that are already the correct length. Calling out to `reallocate()` in this case is a bad idea because there is no guarantee that `reallocate()` won't allocate a new buffer anyway, and based on performance seen in external benchmarks, it seems likely that it is in fact reallocating a new buffer. Before: test string::tests::bench_exact_size_shrink_to_fit ... bench: 45 ns/iter (+/- 2) After: test string::tests::bench_exact_size_shrink_to_fit ... bench: 26 ns/iter (+/- 1) --- src/libcollections/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 689d96b4b295..4c25c9866e47 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -356,7 +356,7 @@ impl Vec { } self.cap = 0; } - } else { + } else if self.cap != self.len { unsafe { // Overflow check is unnecessary as the vector is already at // least this large.