Skip to content

Instantly share code, notes, and snippets.

@yjh0502
Created March 6, 2013 06:57
Show Gist options
  • Save yjh0502/5097287 to your computer and use it in GitHub Desktop.
Save yjh0502/5097287 to your computer and use it in GitHub Desktop.
Rust container growth rate: 2x to 1.5x
diff --git a/src/libcore/at_vec.rs b/src/libcore/at_vec.rs
index d894817..bc3f547 100644
--- a/src/libcore/at_vec.rs
+++ b/src/libcore/at_vec.rs
@@ -269,7 +269,7 @@ pub mod raw {
* * n - The number of elements to reserve space for
*/
pub unsafe fn reserve_at_least<T>(v: &mut @[const T], n: uint) {
- reserve(v, uint::next_power_of_two(n));
+ reserve(v, n + (n>>1));
}
}
diff --git a/src/libcore/hashmap.rs b/src/libcore/hashmap.rs
index 43ec629..65203a3 100644
--- a/src/libcore/hashmap.rs
+++ b/src/libcore/hashmap.rs
@@ -365,7 +365,7 @@ pub mod linear {
fn reserve_at_least(&mut self, n: uint) {
if n > self.buckets.len() {
let buckets = n * 4 / 3 + 1;
- self.resize(uint::next_power_of_two(buckets));
+ self.resize(buckets + (buckets>>1));
}
}
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 19453c5..b7c953d 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -1995,7 +1995,7 @@ pub fn reserve(s: &mut ~str, n: uint) {
* * n - The number of bytes to reserve space for
*/
pub fn reserve_at_least(s: &mut ~str, n: uint) {
- reserve(s, uint::next_power_of_two(n + 1u) - 1u)
+ reserve(s, n + (n>>1) + 1);
}
/**
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs
index ab5f04a..74477b7 100644
--- a/src/libcore/vec.rs
+++ b/src/libcore/vec.rs
@@ -94,7 +94,7 @@ pub fn reserve<T>(v: &mut ~[T], n: uint) {
* * n - The number of elements to reserve space for
*/
pub fn reserve_at_least<T>(v: &mut ~[T], n: uint) {
- reserve(v, uint::next_power_of_two(n));
+ reserve(v, n + (n>>1));
}
/// Returns the number of elements the vector can hold without reallocating
diff --git a/src/libstd/oldmap.rs b/src/libstd/oldmap.rs
index faa26e2..e8f39b2 100644
--- a/src/libstd/oldmap.rs
+++ b/src/libstd/oldmap.rs
@@ -122,7 +122,7 @@ pub mod chained {
fn rehash() {
let n_old_chains = self.chains.len();
- let n_new_chains: uint = uint::next_power_of_two(n_old_chains+1u);
+ let n_new_chains = n_old_chains + (n_old_chains>>1) + 1;
let mut new_chains = chains(n_new_chains);
for self.each_entry |entry| {
let idx = entry.hash % n_new_chains;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment