Skip to content

trait_selection: Restore type-op solver constraints - #163936

Open
Dnreikronos wants to merge 8 commits into
rust-lang:mainfrom
Dnreikronos:trait_selection/restore_type_op_constraints
Open

Dnreikronos wants to merge 8 commits into
rust-lang:mainfrom
Dnreikronos:trait_selection/restore_type_op_constraints

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

View all comments

Bringing back #161423 after @BoxyUwU suggested checking perf again now that Khy's lazy constraint storage from #162531 has landed. The original fix was reverted because of the performance regression.

With assumptions on binders enabled, proving a predicate or normalizing a type can produce solver region constraints inside a canonical query. Those constraints weren't included in the response, so they disappeared when the query's inference context went away. Borrowck could then accept a body with a missing lifetime bound, as in borrowck_env_fail.

The solver constraints now travel in QueryRegionConstraints and get instantiated along with the rest of the response. scrape_region_constraints also drains constraints from completed operations into that same result. Borrowck collects them and lowers them once the body's bounds are available. Constraints from normalizing implied bounds also reach lexical regionck. The cached constraints have no spans, so each caller supplies its own.

I think keeping the constraints in the query response is the right place for this. It keeps the existing query cache and fast paths working, and gives the constraints the same route back to the caller as the other query results. The payload stays optional, so we don't allocate an empty solver constraint tree when AoB is off. I still want a fresh perf run before landing it, since avoiding those allocations alone doesn't tell us what the overall cost is.

The compiler check passed locally, along with the alias-outlives regression, the pass test in default, next-solver and AoB modes, and the three solver-constraint unit tests. Tidy and formatting checks passed too.

r? @BoxyUwU

@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@rustbot rustbot added 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Oct 7, 2026
@BoxyUwU

BoxyUwU commented Oct 7, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 7, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 7, 2026
…onstraints, r=<try>

trait_selection: Restore type-op solver constraints
@rust-bors

rust-bors Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 081f5df (081f5df2a6df38b659cfe0e213131198dda25b5e)
Base parent: 8d1a764 (8d1a76430406c877b35d0b627e7f796dcf0dfeca)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (081f5df): comparison URL.

Overall result: ❌ regressions - 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 @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.3%] 40
Regressions ❌
(secondary)
0.2% [0.0%, 0.3%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.1%, 0.3%] 40

Max RSS (memory usage)

Results (primary 2.1%, secondary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
2.6% [2.6%, 2.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Cycles

Results (secondary 4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.3% [4.3%, 4.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 489.893s -> 488.402s (-0.30%)
Artifact size: 408.60 MiB -> 408.71 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 7, 2026
@BoxyUwU

BoxyUwU commented Oct 7, 2026

Copy link
Copy Markdown
Member

that is significantly better than last time :3 though still subopties

@BoxyUwU

BoxyUwU commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Reading through this I honestly don't know what could be causing the perf regression other than having QueryRegionConstraints be a larger size 🤔 The actual logic being exercise when -Zassumptions-on-binders isn't enabled is largely the same other than a few checks on whether an Option is Some (which it never is) 🤔

I guess my expectation is that this perf regression should disappear at some point over the next month when we merge legacy and abby constraints in the InferCtxt and so can get rid of the legacy constraints from QueryRegionConstraints 🤔 Getting to that point is significantly harder without landing this PR so feel inclined to just merge this despite the perf regression, but that's kind of disappointing and I'd rather not regress stable perf for this 🤔

@rust-bors

This comment has been minimized.

@Dnreikronos

Dnreikronos commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Reading through this I honestly don't know what could be causing the perf regression other than having QueryRegionConstraints be a larger size

Yeah, I'm reading it the same way. With abby off the new code is basically a couple of checks on an Option that's always None, plus an early return. So my guess is the size too. QueryRegionConstraints got one word bigger and it's in every cached type op response, so it might add up.

If you think it's worth it, we could do a try build that only adds the field, always None, nothing else. If that shows the same ~0.2% then it's the layout and the logic itself costs nothing. If it doesn't, then I missed something and I'll go dig for it. Could you queue that one?

Getting to that point is significantly harder without landing this PR so feel inclined to just merge this despite the perf regression, but that's kind of disappointing and I'd rather not regress stable perf for this

Personally I'd rather not add a workaround just to save the size for a month and then rip it out once the legacy constraints are gone. It's a small regression and it has an end date. But I get not wanting to make stable slower for this, so I'm fine either way, your call.

@Dnreikronos
Dnreikronos force-pushed the trait_selection/restore_type_op_constraints branch from 70d2855 to 931b374 Compare October 14, 2026 18:07
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@BoxyUwU

BoxyUwU commented Oct 26, 2026

Copy link
Copy Markdown
Member

@rustbot author

since this is waiting on #163936 (comment)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 26, 2026
@Dnreikronos
Dnreikronos force-pushed the trait_selection/restore_type_op_constraints branch from 931b374 to 3fbd869 Compare October 31, 2026 14:49
@rustbot

This comment has been minimized.

@Dnreikronos
Dnreikronos force-pushed the trait_selection/restore_type_op_constraints branch from 17f5c00 to 347a59a Compare November 26, 2026 00:10
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

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.

@BoxyUwU BoxyUwU left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful this looks good to me, I'm going to run perf one last time to make sure it's the same as before and then r+ this with the small regression with the expectation that it'll go away soon enough when we merge new and old style constraints

View changes since this review

@BoxyUwU

BoxyUwU commented Nov 26, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 26, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…onstraints, r=<try>

trait_selection: Restore type-op solver constraints
@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: e6ba718 (e6ba7185d14f71fe61a6b1ef8ded75c9ace834d3)
Base parent: e068f42 (e068f42013e833ad8c4da25776b7fba798c2ea2d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (e6ba718): comparison URL.

Overall result: ❌ regressions - 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 @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
1.0% [0.1%, 2.9%] 158
Regressions ❌
(secondary)
1.0% [0.0%, 3.0%] 124
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.0% [0.1%, 2.9%] 158

Max RSS (memory usage)

Results (primary 2.0%, secondary 0.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.0% [0.9%, 2.8%] 20
Regressions ❌
(secondary)
1.4% [0.5%, 2.8%] 14
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-2.5%, -0.6%] 3
All ❌✅ (primary) 2.0% [0.9%, 2.8%] 20

Cycles

Results (primary 2.5%, secondary 2.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.5% [2.0%, 3.2%] 22
Regressions ❌
(secondary)
2.9% [0.4%, 5.2%] 26
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.7% [-0.7%, -0.7%] 1
All ❌✅ (primary) 2.5% [2.0%, 3.2%] 22

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 461.814s -> 463.145s (0.29%)
Artifact size: 405.58 MiB -> 405.70 MiB (0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 26, 2026
@BoxyUwU

BoxyUwU commented Nov 26, 2026

Copy link
Copy Markdown
Member

why is it vastly worse lmao

@BoxyUwU

BoxyUwU commented Nov 27, 2026

Copy link
Copy Markdown
Member

seems to just be a lot of time spent in add_solver_constraints, I wonder if a simple fast path of "return if the constraint is true" would make this fine again

@Dnreikronos

Copy link
Copy Markdown
Contributor Author

seems to just be a lot of time spent in add_solver_constraints, I wonder if a simple fast path of "return if the constraint is true" would make this fine again

Yeah, that looks like most of it. When I dropped Option<Box>, I forgot to put is_true checks back in instantiate_nll_query_response_and_region_obligations and extend on QueryRegionConstraints. Since a true Or still holds one empty And, every NLL query was calling add_solver_constraints and hitting build_and, which looped and allocated a Vec on every call even with abby off.

I tested adding the is_true early return in add_solver_constraints locally, along with guarding those two callers so we skip the clone and instantiate_value before reaching add_solver_constraints. On a quick local serde check against main, that cut out most of the jump, dropping from roughly 2.5% down to around 0.5%.

While testing I noticed there was still a small leftover bump on instructions and around 2% on max RSS compared to main. Out of curiosity I also tried putting Option<Box> back and that brought serde down near 0.3% and basically flat on memory, probably because RegionConstraint is pretty wide inline now with the SmallVecs from #164314 sitting in every cached query response.

Personally I'd push the fast path first so we keep the simpler code without Option<Box>, and see what a real perf run says. If the leftover bump or RSS still looks too high there, we can always bring Option<Box> back until the constraints get merged. Want me to push the fast path so we can try perf again?

@BoxyUwU

BoxyUwU commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

yeah if you could push that'd be great, ill either start a perf run myself or you can ask in #t-compiler/help > perf run for someone else to start one as it might take me longer to see the notification :)

@panstromek

Copy link
Copy Markdown
Contributor

@bors try @rust-timer queue

@rust-timer

This comment was marked as outdated.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
…onstraints, r=<try>

trait_selection: Restore type-op solver constraints
@panstromek

Copy link
Copy Markdown
Contributor

@rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 28, 2026
@rust-bors

rust-bors Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 6abad6d (6abad6dad50ac4263f9017800cc6591f2221c0db)
Base parent: 7e3e8d5 (7e3e8d5c195e762aebced8aafeb49f929f6c11ab)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (6abad6d): comparison URL.

Overall result: ❌ regressions - 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 @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.4% [0.2%, 0.6%] 56
Regressions ❌
(secondary)
0.4% [0.2%, 0.5%] 41
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.2%, 0.6%] 56

Max RSS (memory usage)

Results (primary 2.0%, secondary 1.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.0% [0.9%, 3.7%] 28
Regressions ❌
(secondary)
1.4% [0.5%, 2.7%] 15
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.0% [0.9%, 3.7%] 28

Cycles

Results (primary -2.6%, secondary 0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.0% [0.4%, 2.1%] 5
Improvements ✅
(primary)
-2.6% [-2.6%, -2.6%] 1
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.5%] 1
All ❌✅ (primary) -2.6% [-2.6%, -2.6%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 463.544s -> 461.848s (-0.37%)
Artifact size: 406.56 MiB -> 406.65 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 28, 2026

This branch has not been deployed

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

Labels

perf-regression Performance regression. 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants