Skip to content

Perf: Only scan the affected suffix of the provisional cache - #161130

Open
Dandandan wants to merge 1 commit into
rust-lang:mainfrom
Dandandan:provisional-cache-suffix-scan
Open

Perf: Only scan the affected suffix of the provisional cache#161130
Dandandan wants to merge 1 commit into
rust-lang:mainfrom
Dandandan:provisional-cache-suffix-scan

Conversation

@Dandandan

@Dandandan Dandandan commented Aug 15, 2026

Copy link
Copy Markdown

insert_provisional walks the whole provisional cache on every insert, to lower
reached_depth on entries at or above from_dfn. Everything it can affect was
inserted while from_dfn was on the stack, so those entries are a suffix of this
insertion-ordered map, and the walk can stop at the first older entry.

The full walk is quadratic in the size of the cache. That cache only fills up for
mutually recursive types, so this shows up when proving Send or Sync for a
graph of such types: for a graph of N of them the loop ran about N² times.

evaluate_obligation self time on that graph:

types before after
60 104ms 61ms
120 315ms 118ms
240 886ms 243ms
480 3.06s 0.55s

Growth goes from superlinear to linear, which is what the same graph already does
when it has no cycles.

I ran into this looking at DataFusion, whose Expr and LogicalPlan types are
mutually recursive across a few hundred types.

AI Disclosure

I used LLM (Claude) to run some experiments and change the code

`insert_provisional` walks the whole provisional cache on every insert to
lower `reached_depth`. Everything it can affect was inserted while
`from_dfn` was on the stack, so it is a suffix of this insertion-ordered
map and the walk can stop at the first older entry.

The full walk is quadratic in the size of the cache, which only fills up
for mutually recursive types. Proving `Send` for a graph of N such types
did about N^2 iterations of this loop -- 641M of them for N=480.

`evaluate_obligation` on that graph:

    N=60   104ms ->  61ms
    N=120  315ms -> 118ms
    N=240  886ms -> 243ms
    N=480  3.06s -> 0.55s

That is the linear growth the same graph already has when it is acyclic.
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 15, 2026
@Dandandan Dandandan changed the title Only scan the affected suffix of the provisional cache Perf: Only scan the affected suffix of the provisional cache Aug 15, 2026
@Dandandan
Dandandan marked this pull request as ready for review August 15, 2026 09:47
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 15, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 15, 2026
@rustbot

rustbot commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @nnethercote (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 75 candidates
  • Random selection from 19 candidates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants