Skip to content

Commit 8752d61

Browse files
committed
perf: Reduce allocations in try_evaluate_obligations
1 parent 51d4227 commit 8752d61

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

crates/hir-ty/src/next_solver/fulfill.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod errors;
44

5-
use std::{mem, ops::ControlFlow};
5+
use std::ops::ControlFlow;
66

77
use rustc_hash::FxHashSet;
88
use rustc_next_trait_solver::{
@@ -77,14 +77,12 @@ impl<'db> ObligationStorage<'db> {
7777
obligations
7878
}
7979

80-
fn drain_pending(
81-
&mut self,
82-
cond: impl Fn(&PredicateObligation<'db>) -> bool,
83-
) -> PendingObligations<'db> {
84-
let (not_stalled, pending) =
85-
mem::take(&mut self.pending).into_iter().partition(|(o, _)| cond(o));
86-
self.pending = pending;
87-
not_stalled
80+
fn drain_pending<'this, 'cond>(
81+
&'this mut self,
82+
cond: impl 'cond + Fn(&PredicateObligation<'db>) -> bool,
83+
) -> impl Iterator<Item = (PredicateObligation<'db>, Option<GoalStalledOn<DbInterner<'db>>>)>
84+
{
85+
self.pending.extract_if(.., move |(o, _)| cond(o))
8886
}
8987

9088
fn on_fulfillment_overflow(&mut self, infcx: &InferCtxt<'db>) {
@@ -165,9 +163,11 @@ impl<'db> FulfillmentCtxt<'db> {
165163
// to not put the obligations queue in `InferenceTable`'s snapshots.
166164
// assert_eq!(self.usable_in_snapshot, infcx.num_open_snapshots());
167165
let mut errors = Vec::new();
166+
let mut obligations = Vec::new();
168167
loop {
169168
let mut any_changed = false;
170-
for (mut obligation, stalled_on) in self.obligations.drain_pending(|_| true) {
169+
obligations.extend(self.obligations.drain_pending(|_| true));
170+
for (mut obligation, stalled_on) in obligations.drain(..) {
171171
if obligation.recursion_depth >= infcx.interner.recursion_limit() {
172172
self.obligations.on_fulfillment_overflow(infcx);
173173
// Only return true errors that we have accumulated while processing.
@@ -269,7 +269,6 @@ impl<'db> FulfillmentCtxt<'db> {
269269
.is_break()
270270
})
271271
})
272-
.into_iter()
273272
.map(|(o, _)| o)
274273
.collect()
275274
}

0 commit comments

Comments
 (0)