Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing allocations using Stream::size_hint #287

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/collections/binary_heap/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + '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)))
}
}
13 changes: 6 additions & 7 deletions src/collections/hash_map/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions src/collections/hash_set/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/collections/linked_list/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ impl<T> Extend<T> for LinkedList<T> {
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + '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);
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
Box::pin(stream.for_each(move |item| self.push_back(item)))
}
}
6 changes: 3 additions & 3 deletions src/collections/vec_deque/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ impl<T> Extend<T> for VecDeque<T> {
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + '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)))
}
}
5 changes: 2 additions & 3 deletions src/string/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ impl Extend<char> for String {
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + '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)))
}
Expand Down
6 changes: 3 additions & 3 deletions src/vec/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ impl<T> Extend<T> for Vec<T> {
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + '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)))
}
}