forked from mirror/async-std
Merge pull request #287 from k-nasa/optimizing_allocations
Optimizing allocations using Stream::size_hint
This commit is contained in:
commit
460b8af50b
7 changed files with 23 additions and 29 deletions
|
@ -10,9 +10,9 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
||||||
let stream = stream.into_stream();
|
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(stream.size_hint().0);
|
||||||
//self.reserve(lower_bound);
|
|
||||||
Box::pin(stream.for_each(move |item| self.push(item)))
|
Box::pin(stream.for_each(move |item| self.push(item)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,13 +23,12 @@ where
|
||||||
// hint lower bound if the map is empty. Otherwise reserve half the hint (rounded up), so
|
// 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.
|
// the map will only resize twice in the worst case.
|
||||||
|
|
||||||
//TODO: Add this back in when size_hint is added to Stream/StreamExt
|
let additional = if self.is_empty() {
|
||||||
//let reserve = if self.is_empty() {
|
stream.size_hint().0
|
||||||
// stream.size_hint().0
|
} else {
|
||||||
//} else {
|
(stream.size_hint().0 + 1) / 2
|
||||||
// (stream.size_hint().0 + 1) / 2
|
};
|
||||||
//};
|
self.reserve(additional);
|
||||||
//self.reserve(reserve);
|
|
||||||
|
|
||||||
Box::pin(stream.for_each(move |(k, v)| {
|
Box::pin(stream.for_each(move |(k, v)| {
|
||||||
self.insert(k, v);
|
self.insert(k, v);
|
||||||
|
|
|
@ -26,13 +26,12 @@ where
|
||||||
// hint lower bound if the map is empty. Otherwise reserve half the hint (rounded up), so
|
// 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.
|
// the map will only resize twice in the worst case.
|
||||||
|
|
||||||
//TODO: Add this back in when size_hint is added to Stream/StreamExt
|
let additional = if self.is_empty() {
|
||||||
//let reserve = if self.is_empty() {
|
stream.size_hint().0
|
||||||
// stream.size_hint().0
|
} else {
|
||||||
//} else {
|
(stream.size_hint().0 + 1) / 2
|
||||||
// (stream.size_hint().0 + 1) / 2
|
};
|
||||||
//};
|
self.reserve(additional);
|
||||||
//self.reserve(reserve);
|
|
||||||
|
|
||||||
Box::pin(stream.for_each(move |item| {
|
Box::pin(stream.for_each(move |item| {
|
||||||
self.insert(item);
|
self.insert(item);
|
||||||
|
|
|
@ -10,9 +10,6 @@ impl<T> Extend<T> for LinkedList<T> {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
||||||
let stream = stream.into_stream();
|
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)))
|
Box::pin(stream.for_each(move |item| self.push_back(item)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@ impl<T> Extend<T> for VecDeque<T> {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
||||||
let stream = stream.into_stream();
|
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(stream.size_hint().0);
|
||||||
//self.reserve(lower_bound);
|
|
||||||
Box::pin(stream.for_each(move |item| self.push_back(item)))
|
Box::pin(stream.for_each(move |item| self.push_back(item)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,8 @@ impl Extend<char> for String {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
||||||
let stream = stream.into_stream();
|
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(stream.size_hint().0);
|
||||||
// self.reserve(lower_bound);
|
|
||||||
|
|
||||||
Box::pin(stream.for_each(move |c| self.push(c)))
|
Box::pin(stream.for_each(move |c| self.push(c)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@ impl<T> Extend<T> for Vec<T> {
|
||||||
stream: S,
|
stream: S,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
|
||||||
let stream = stream.into_stream();
|
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(stream.size_hint().0);
|
||||||
//self.reserve(lower_bound);
|
|
||||||
Box::pin(stream.for_each(move |item| self.push(item)))
|
Box::pin(stream.for_each(move |item| self.push(item)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue