Skip to content

Commit 3e13393

Browse files
authored
Rollup merge of rust-lang#53019 - ljedrz:bad_collects, r=estebank
Don't collect() when size_hint is useless This adjusts PRs rust-lang#52738 and rust-lang#52697 by falling back to calculating capacity and extending or pushing in a loop where `collect()` can't be trusted to calculate the right capacity. It is a performance win.
2 parents a77dfcc + b68b396 commit 3e13393

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/librustc/infer/outlives/obligations.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
151151
debug!("process_registered_region_obligations()");
152152

153153
// pull out the region obligations with the given `body_id` (leaving the rest)
154-
let my_region_obligations = {
154+
let mut my_region_obligations = Vec::with_capacity(self.region_obligations.borrow().len());
155+
{
155156
let mut r_o = self.region_obligations.borrow_mut();
156-
let my_r_o = r_o.drain_filter(|(ro_body_id, _)| *ro_body_id == body_id)
157-
.map(|(_, obligation)| obligation).collect::<Vec<_>>();
158-
my_r_o
159-
};
157+
my_region_obligations.extend(
158+
r_o.drain_filter(|(ro_body_id, _)| *ro_body_id == body_id)
159+
.map(|(_, obligation)| obligation)
160+
);
161+
}
160162

161163
let outlives = &mut TypeOutlives::new(
162164
self,

src/librustc_data_structures/small_vec.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ impl<A> Decodable for SmallVec<A>
210210
A::Element: Decodable {
211211
fn decode<D: Decoder>(d: &mut D) -> Result<SmallVec<A>, D::Error> {
212212
d.read_seq(|d, len| {
213-
(0..len).map(|i| d.read_seq_elt(i, |d| Decodable::decode(d))).collect()
213+
let mut vec = SmallVec::with_capacity(len);
214+
// FIXME(#48994) - could just be collected into a Result<SmallVec, D::Error>
215+
for i in 0..len {
216+
vec.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
217+
}
218+
Ok(vec)
214219
})
215220
}
216221
}

src/libsyntax/ast.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,11 @@ impl Pat {
501501
PatKind::Slice(pats, None, _) if pats.len() == 1 =>
502502
pats[0].to_ty().map(TyKind::Slice)?,
503503
PatKind::Tuple(pats, None) => {
504-
let tys = pats.iter().map(|pat| pat.to_ty()).collect::<Option<Vec<_>>>()?;
504+
let mut tys = Vec::with_capacity(pats.len());
505+
// FIXME(#48994) - could just be collected into an Option<Vec>
506+
for pat in pats {
507+
tys.push(pat.to_ty()?);
508+
}
505509
TyKind::Tup(tys)
506510
}
507511
_ => return None,

0 commit comments

Comments
 (0)