Skip to content

Commit d8b4abc

Browse files
authored
Rollup merge of #69022 - ljedrz:traits_tweak_vecs, r=petrochenkov
traits: preallocate 2 Vecs of known initial size The 2 preallocations are pretty obvious; both vectors will be as big as or larger than the collections they are created from. In `WfPredicates::normalize` the change from a functional style improves readability and should be perf-friendly, too.
2 parents c8c2b2b + b8893df commit d8b4abc

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/librustc/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3472,7 +3472,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
34723472
// that order.
34733473
let predicates = tcx.predicates_of(def_id);
34743474
assert_eq!(predicates.parent, None);
3475-
let mut obligations = Vec::new();
3475+
let mut obligations = Vec::with_capacity(predicates.predicates.len());
34763476
for (predicate, _) in predicates.predicates {
34773477
let predicate = normalize_with_depth_to(
34783478
self,

src/librustc/traits/wf.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,15 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
143143
let cause = self.cause(traits::MiscObligation);
144144
let infcx = &mut self.infcx;
145145
let param_env = self.param_env;
146-
let mut obligations = Vec::new();
147-
self.out.iter().inspect(|pred| assert!(!pred.has_escaping_bound_vars())).for_each(|pred| {
146+
let mut obligations = Vec::with_capacity(self.out.len());
147+
for pred in &self.out {
148+
assert!(!pred.has_escaping_bound_vars());
148149
let mut selcx = traits::SelectionContext::new(infcx);
149150
let i = obligations.len();
150151
let value =
151152
traits::normalize_to(&mut selcx, param_env, cause.clone(), pred, &mut obligations);
152153
obligations.insert(i, value);
153-
});
154+
}
154155
obligations
155156
}
156157

0 commit comments

Comments
 (0)