Avoid redundant next-solver fulfillment scans - #161348
Conversation
|
@nnethercote I avoided some of the redundant fulfillment checks where there was no change in the inference state |
|
I'm not expert enough with the trait solver to evaluate this. (@lcnr or @jdonszelmann might have opinions.) It does look like a fairly complex change without much explanation. Did you write the code yourself? |
I used the stalled goal fast path from #158249 and the try_evaluate_obligations work in #160479 |
|
this seems vaguely appropriate, the core idea "fast path if literally no infer var changed since last run" makes a lot of sense to me. The way it's written right now feels very brittle and I would like to encapsulate this somehow in a way that makes it harder to accidentally forget to check/update something. I considered separately having a shared list of what every goal in the fulfillment context is stalled on, so we dont need to iterate over it if literally nothing in it changed even if unrelated infer vars changed. Why do you not reset this counter when rolling back a snapshot. There's a lot of open design space here, think this is definitely a good direction :> |
|
One option is to store the "state after last fulfillment loop" in the That way you could also track "highest mentioned infer var indices" and "lowest constrained infer var" to ignore any constraints for infer vars which the fulfillment context is not stalled on. Idk if that matters :> |
|
@lcnr Thanks for your review I agree about the brittleness though |
but why? rolling them back should be completely correct, should it not? |
I am still unsure about this part but I think you are right here |
aec0740 to
20985b8
Compare
|
Some changes occurred to the core trait solver cc @rust-lang/initiative-trait-system-refactor |
|
r? @wesleywiser rustbot has assigned @wesleywiser. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
@rustbot ready |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Avoid redundant next-solver fulfillment scans
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (5d793fd): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. Next, please: If you can, justify the regressions found in this try perf run in writing along with @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -1.5%, secondary -0.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary 3.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 456.256s -> 463.808s (1.66%) |
20985b8 to
3761377
Compare
|
@lcnr do you think the InferCtxt state + mem::swap approach would be cleaner than keeping revisions in TypeVariableStorage ? 🤔 |
| fn bump_stalled_goal_revision(&mut self) { | ||
| let revision = self.storage.stalled_goal_revision; | ||
| self.undo_log.push(UndoLog::StalledGoalRevision(revision)); | ||
| self.storage.stalled_goal_revision = revision.wrapping_add(1); |
There was a problem hiding this comment.
you don't need to store this in the undo log 🤔 you can instead store the revision in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/snapshot/undo_log/struct.Snapshot.html and update the revision in the infcx when rolling it back.
| stalled_sub_roots: &mut FxIndexSet<ty::TyVid>, | ||
| stalled_on_empty_opaques: &mut bool, | ||
| ) -> bool { | ||
| if stalled_on.stalled_vars.iter().any(|var| !matches!(*var, TyOrConstInferVar::Ty(_))) { |
There was a problem hiding this comment.
that feels off, why not also track const vars? or well, probably easiest to increment the counter when doing any of
- sub unify
- constrain const / type infer var
There was a problem hiding this comment.
I think TyInt and TyFloat should bump it too alongside Ty/Const. they're part of TyOrConstInferVar and resolving any of them can make a stalled goal runnable again
keeping them out would leave the context-wide fast path with a second correctness condition while including them gives us one clear invalidation invariant
That seems like the cleaner model to me
What do you think?
maybe? 🤷 seems worth to try to make it as "nice" as possible and see what works best. Or maybe experiment with both in case one of them is faster |
3761377 to
4906be1
Compare
|
changes to the core type system cc @lcnr |
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@lcnr I ended up using one coarse generation for type constraints and unification. now it stored in the snapshot now and restored on rollback and there’s no undo-log entry for every bump this way was a bit faster locally and feels much simpler |
View all comments
Refs #159933
The slowdown here comes from repeatedly walking the pending fulfillment queue even when nothing has changed that could make the stalled goals progress which this adds a generation to the inference context which is bumped whenever we constrain type, const, int and float inference variables or do type sub unification
fulfillment remembers the generation after reaching a fixpoint and can skip walking the queue again if the generation is unchanged and the pending goals are safe to track this way
The generation is part of the snapshot state and rolling back a snapshot restores it with the corresponding inference state which now we avoid putting every generation bump into the undo log
On the reproducer from issue against current status: