Skip to content

Commit 2d8d5b7

Browse files
committed
Improve readability of needs_process_obligation.
And add some useful comments elsewhere.
1 parent cec4c4d commit 2d8d5b7

File tree

2 files changed

+25
-17
lines changed
  • compiler
    • rustc_data_structures/src/obligation_forest
    • rustc_trait_selection/src/traits

2 files changed

+25
-17
lines changed

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ impl<O: ForestObligation> ObligationForest<O> {
426426
// nodes. Therefore we use a `while` loop.
427427
let mut index = 0;
428428
while let Some(node) = self.nodes.get_mut(index) {
429+
// This test is extremely hot.
429430
if node.state.get() != NodeState::Pending
430431
|| !processor.needs_process_obligation(&node.obligation)
431432
{
@@ -439,6 +440,7 @@ impl<O: ForestObligation> ObligationForest<O> {
439440
// out of sync with `nodes`. It's not very common, but it does
440441
// happen, and code in `compress` has to allow for it.
441442

443+
// This code is much less hot.
442444
match processor.process_obligation(&mut node.obligation) {
443445
ProcessResult::Unchanged => {
444446
// No change in state.

compiler/rustc_trait_selection/src/traits/fulfill.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,31 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
212212

213213
/// Identifies whether a predicate obligation needs processing.
214214
///
215-
/// This is always inlined, despite its size, because it has a single
216-
/// callsite and it is called *very* frequently.
215+
/// This is always inlined because it has a single callsite and it is
216+
/// called *very* frequently. Be careful modifying this code! Several
217+
/// compile-time benchmarks are very sensitive to even small changes.
217218
#[inline(always)]
218219
fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
219-
// If we were stalled on some unresolved variables, first check whether
220-
// any of them have been resolved; if not, don't bother doing more work
221-
// yet.
222-
match pending_obligation.stalled_on.len() {
223-
// Match arms are in order of frequency, which matters because this
224-
// code is so hot. 1 and 0 dominate; 2+ is fairly rare.
225-
1 => {
226-
let infer_var = pending_obligation.stalled_on[0];
227-
self.selcx.infcx.inlined_ty_or_const_infer_var_changed(infer_var)
228-
}
229-
0 => {
230-
// In this case we haven't changed, but wish to make a change.
231-
true
232-
}
233-
_ => pending_obligation.stalled_on.iter().any(|&infer_var| {
220+
let stalled_on = &pending_obligation.stalled_on;
221+
match stalled_on.len() {
222+
// This case is the hottest most of the time, being hit up to 99%
223+
// of the time. `keccak` and `cranelift-codegen-0.82.1` are
224+
// benchmarks that particularly stress this path.
225+
1 => self.selcx.infcx.inlined_ty_or_const_infer_var_changed(stalled_on[0]),
226+
227+
// In this case we haven't changed, but wish to make a change. Note
228+
// that this is a special case, and is not equivalent to the `_`
229+
// case below, which would return `false` for an empty `stalled_on`
230+
// vector.
231+
//
232+
// This case is usually hit only 1% of the time or less, though it
233+
// reaches 20% in `wasmparser-0.101.0`.
234+
0 => true,
235+
236+
// This case is usually hit only 1% of the time or less, though it
237+
// reaches 95% in `mime-0.3.16`, 64% in `wast-54.0.0`, and 12% in
238+
// `inflate-0.4.5`.
239+
_ => stalled_on.iter().any(|&infer_var| {
234240
self.selcx.infcx.uninlined_ty_or_const_infer_var_changed(infer_var)
235241
}),
236242
}

0 commit comments

Comments
 (0)