Skip to content

Perf: Clear the param env for more global trait goals - #161126

Open
Dandandan wants to merge 1 commit into
rust-lang:mainfrom
Dandandan:clear-param-env-for-global-goals
Open

Perf: Clear the param env for more global trait goals#161126
Dandandan wants to merge 1 commit into
rust-lang:mainfrom
Dandandan:clear-param-env-for-global-goals

Conversation

@Dandandan

@Dandandan Dandandan commented Aug 15, 2026

Copy link
Copy Markdown

evaluate_trait_predicate_recursively clears the param env when its bounds cannot
affect a global goal, so the result can go in the global cache. It almost never
fires today, for two reasons:

  • It asks obligation.is_global(). An Obligation visits its own param_env, so
    this is false whenever the env holds anything local, including the region
    inference variables the branch is about to discard. In practice it could only
    fire when the env was already empty.

  • It requires every bound to mention a generic parameter. An outlives bound is
    never a selection candidate, so it cannot change the answer either.

Code using #[async_trait] runs into this. Every method there carries
where 'life0: 'async_trait, .., and those bounds hold region inference
variables, so each method re-proves the same Send/Sync goals from scratch.

On a generated test file with an async_trait-shaped trait over a large concrete
type graph, evaluate_obligation self time drops:

methods before after
1 150ms 103ms
10 594ms 449ms
30 1.56s 1.08s

I found this while looking at compile times in DataFusion, where a few crates
spend most of their time proving Send for futures returned by #[async_trait]
methods.

AI Disclosure

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

The branch in `evaluate_trait_predicate_recursively` that clears the param
env, so a global goal can be cached globally, rarely fires:

- it asks `obligation.is_global()`, but an `Obligation` visits its own
  `param_env`, so that is false whenever the env holds anything local --
  including the region inference variables the branch then discards. It
  could only fire when the env was already empty.

- it requires every bound to mention a generic parameter. An outlives bound
  is never a selection candidate, so it cannot change the answer either.

Code using `#[async_trait]` runs into this: every method carries
`where 'life0: 'async_trait, ..`, whose bounds hold region inference
variables, so each method re-proves the same `Send`/`Sync` goals.
@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 Clear the param env for more global trait goals Perf: Clear the param env for more global trait goals Aug 15, 2026
@Dandandan
Dandandan force-pushed the clear-param-env-for-global-goals branch from 892ca2d to 2850f83 Compare August 15, 2026 09:23
@Dandandan
Dandandan marked this pull request as ready for review August 15, 2026 09:47
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels 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 @petrochenkov (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