From 9cede7f1e33aae30a71678224c9f70c4a5297952 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 8 Oct 2019 00:06:49 +0900 Subject: [PATCH 1/2] refactor: optimizing allocations --- src/collections/binary_heap/extend.rs | 6 +++--- src/collections/hash_map/extend.rs | 13 ++++++------- src/collections/hash_set/extend.rs | 13 ++++++------- src/collections/linked_list/extend.rs | 3 --- src/collections/vec_deque/extend.rs | 6 +++--- src/string/extend.rs | 5 ++--- src/vec/extend.rs | 6 +++--- 7 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/collections/binary_heap/extend.rs b/src/collections/binary_heap/extend.rs index 8538f9b0c..4503fe393 100644 --- a/src/collections/binary_heap/extend.rs +++ b/src/collections/binary_heap/extend.rs @@ -10,9 +10,9 @@ impl Extend for BinaryHeap { stream: S, ) -> Pin + 'a>> { let stream = stream.into_stream(); - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let (lower_bound, _) = stream.size_hint(); - //self.reserve(lower_bound); + + self.reserve(stream.size_hint().0); + Box::pin(stream.for_each(move |item| self.push(item))) } } diff --git a/src/collections/hash_map/extend.rs b/src/collections/hash_map/extend.rs index 5f96b8abe..d7ca2b946 100644 --- a/src/collections/hash_map/extend.rs +++ b/src/collections/hash_map/extend.rs @@ -23,13 +23,12 @@ where // hint lower bound if the map is empty. Otherwise reserve half the hint (rounded up), so // the map will only resize twice in the worst case. - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let reserve = if self.is_empty() { - // stream.size_hint().0 - //} else { - // (stream.size_hint().0 + 1) / 2 - //}; - //self.reserve(reserve); + let additional = if self.is_empty() { + stream.size_hint().0 + } else { + (stream.size_hint().0 + 1) / 2 + }; + self.reserve(additional); Box::pin(stream.for_each(move |(k, v)| { self.insert(k, v); diff --git a/src/collections/hash_set/extend.rs b/src/collections/hash_set/extend.rs index 72400e11f..e10cb3c6c 100644 --- a/src/collections/hash_set/extend.rs +++ b/src/collections/hash_set/extend.rs @@ -26,13 +26,12 @@ where // hint lower bound if the map is empty. Otherwise reserve half the hint (rounded up), so // the map will only resize twice in the worst case. - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let reserve = if self.is_empty() { - // stream.size_hint().0 - //} else { - // (stream.size_hint().0 + 1) / 2 - //}; - //self.reserve(reserve); + let additional = if self.is_empty() { + stream.size_hint().0 + } else { + (stream.size_hint().0 + 1) / 2 + }; + self.reserve(additional); Box::pin(stream.for_each(move |item| { self.insert(item); diff --git a/src/collections/linked_list/extend.rs b/src/collections/linked_list/extend.rs index 567a92d30..63a1a97c3 100644 --- a/src/collections/linked_list/extend.rs +++ b/src/collections/linked_list/extend.rs @@ -10,9 +10,6 @@ impl Extend for LinkedList { stream: S, ) -> Pin + 'a>> { let stream = stream.into_stream(); - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let (lower_bound, _) = stream.size_hint(); - //self.reserve(lower_bound); Box::pin(stream.for_each(move |item| self.push_back(item))) } } diff --git a/src/collections/vec_deque/extend.rs b/src/collections/vec_deque/extend.rs index d034bdcd9..17e396ab8 100644 --- a/src/collections/vec_deque/extend.rs +++ b/src/collections/vec_deque/extend.rs @@ -10,9 +10,9 @@ impl Extend for VecDeque { stream: S, ) -> Pin + 'a>> { let stream = stream.into_stream(); - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let (lower_bound, _) = stream.size_hint(); - //self.reserve(lower_bound); + + self.reserve(stream.size_hint().0); + Box::pin(stream.for_each(move |item| self.push_back(item))) } } diff --git a/src/string/extend.rs b/src/string/extend.rs index 72260a546..8572cc3ce 100644 --- a/src/string/extend.rs +++ b/src/string/extend.rs @@ -10,9 +10,8 @@ impl Extend for String { stream: S, ) -> Pin + 'a>> { let stream = stream.into_stream(); - //TODO: Add this back in when size_hint is added to Stream/StreamExt - // let (lower_bound, _) = stream.size_hint(); - // self.reserve(lower_bound); + + self.reserve(stream.size_hint().0); Box::pin(stream.for_each(move |c| self.push(c))) } diff --git a/src/vec/extend.rs b/src/vec/extend.rs index ca3373d68..ecf68c83f 100644 --- a/src/vec/extend.rs +++ b/src/vec/extend.rs @@ -9,9 +9,9 @@ impl Extend for Vec { stream: S, ) -> Pin + 'a>> { let stream = stream.into_stream(); - //TODO: Add this back in when size_hint is added to Stream/StreamExt - //let (lower_bound, _) = stream.size_hint(); - //self.reserve(lower_bound); + + self.reserve(stream.size_hint().0); + Box::pin(stream.for_each(move |item| self.push(item))) } } From 1c798387bfe568bf027f1d188259b2e133e2fcd8 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 8 Oct 2019 10:50:30 +0900 Subject: [PATCH 2/2] $cargo fmt --- src/collections/hash_map/extend.rs | 4 ++-- src/collections/hash_set/extend.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/collections/hash_map/extend.rs b/src/collections/hash_map/extend.rs index d7ca2b946..c34bb9ed3 100644 --- a/src/collections/hash_map/extend.rs +++ b/src/collections/hash_map/extend.rs @@ -24,9 +24,9 @@ where // the map will only resize twice in the worst case. let additional = if self.is_empty() { - stream.size_hint().0 + stream.size_hint().0 } else { - (stream.size_hint().0 + 1) / 2 + (stream.size_hint().0 + 1) / 2 }; self.reserve(additional); diff --git a/src/collections/hash_set/extend.rs b/src/collections/hash_set/extend.rs index e10cb3c6c..123e844e2 100644 --- a/src/collections/hash_set/extend.rs +++ b/src/collections/hash_set/extend.rs @@ -27,9 +27,9 @@ where // the map will only resize twice in the worst case. let additional = if self.is_empty() { - stream.size_hint().0 + stream.size_hint().0 } else { - (stream.size_hint().0 + 1) / 2 + (stream.size_hint().0 + 1) / 2 }; self.reserve(additional);