Skip to content

Manage pending better in try_evaluate_obligations - #160418

Open
nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:pending-cap
Open

Manage pending better in try_evaluate_obligations#160418
nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:pending-cap

Conversation

@nnethercote

Copy link
Copy Markdown
Contributor

For extreme new-solver cases like nacl-0.5.3 and ijson-0.1.6 the loop in try_evaluate_obligations can be very inefficient. This commit makes a very simple capacity tweak to the pending vec to fix that.

There are some other possible changes to this function to improve performance further but they are more complicated so I'm doing this easy one first.

r? @lcnr

For extreme new-solver cases like `nacl-0.5.3` and `ijson-0.1.6` the
loop in `try_evaluate_obligations` can be very inefficient. This commit
makes a very simple capacity tweak to the `pending` vec to fix that.

There are some other possible changes to this function to improve
performance further but they are more complicated so I'm doing this easy
one first.
@rustbot

rustbot commented Aug 3, 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 Aug 3, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 3, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 3, 2026
Manage `pending` better in `try_evaluate_obligations`
@lcnr

lcnr commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

r? @jdonszelmann

@rustbot rustbot assigned jdonszelmann and unassigned lcnr Aug 3, 2026
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

jdonszelmann is currently at their maximum review capacity.
They may take a while to respond.

@rust-bors

rust-bors Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: f3c3930 (f3c393027d1cc38eb8cffcf8b8bd386071bafc75)
Base parent: 65dd30f (65dd30fb9e882a7e8f0be10caca62936db2a98b8)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (f3c3930): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

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

Max RSS (memory usage)

Results (primary 3.7%, secondary -0.5%)

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

mean range count
Regressions ❌
(primary)
3.7% [3.7%, 3.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.7%, -0.4%] 11
All ❌✅ (primary) 3.7% [3.7%, 3.7%] 1

Cycles

Results (primary -0.4%, 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)
0.7% [0.7%, 0.7%] 2
Regressions ❌
(secondary)
2.2% [0.9%, 3.4%] 2
Improvements ✅
(primary)
-0.7% [-1.6%, -0.4%] 7
Improvements ✅
(secondary)
-1.4% [-3.7%, -0.5%] 11
All ❌✅ (primary) -0.4% [-1.6%, 0.7%] 9

Binary size

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

Bootstrap: 491.898s -> 487.335s (-0.93%)
Artifact size: 391.08 MiB -> 390.28 MiB (-0.21%)

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

Copy link
Copy Markdown
Contributor Author

The instruction count results look neutral, but if we look at cycles for new-solver benchmarks it's a bit different: there's some green there, with the biggest reductions on wg-grammar (as expected).

I also measured locally, including the extreme nacl-0.5.3 and ijson-0.1.6 and nvml-wrapper-sys examples (which show the effects much more). Instruction counts:

image

And cycles:

image

I think there's a genuine effect here on the extreme cases.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Oh, and it makes sense for the cycle count improvements to exceed instruction count improvements. When building up the pending vec by pushing, there are a lot of reallocations, which involve memcpy, which on x86-64 can be done with REP instructions where a single instruction can do many iterations and thus take many cycles.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Also, when I measured with Cachegrind, nacl-0.5.3 went from 54.6 billion instructions to 41.5 billion instructions. Cachegrind is different to perf in that it counts each iteration of a REP instruction separately, which explains why the reduction is so much larger.

let pending = mem::take(&mut self.obligations.pending);
self.obligations.pending.reserve(pending.capacity());

for (mut obligation, stalled_on) in pending {

@lcnr lcnr Aug 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we rewrite this as a retain instead? i think that's what waffle's PR #158126 does?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

actually no, @WaffleLapkin's PR only does this in the trait solver internal loop, which I think is significantly less hot?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Something like that. That's the "other possible changes" I mentioned in the PR description.

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. 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.

5 participants