Skip to content

Avoid allocations when canonicalizing - #161077

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nnethercote:CanonicalizerState
Aug 14, 2026
Merged

Avoid allocations when canonicalizing#161077
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nnethercote:CanonicalizerState

Conversation

@nnethercote

@nnethercote nnethercote commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Canonicalization is hot in the new solver. This commit avoids some allocations while doing it. Details in individual commits.

r? @lcnr

There are many short lived `Canonicalizer` instances. These each involve
some vecs and hashmaps, and the cost of the allocations for these adds
up. This commit introduces `CanonicalizerState` which holds the vecs and
hashmaps, and puts a single instance in `InferCtxt` to be cleared and
reused for each canonicalizer.
It's a small performance win. This requires adding `DelayedMap::clear`
(and `DelayedSet::clear` is also added for symmetry).
@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Aug 13, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

@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 Aug 13, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 13, 2026
Avoid allocations when canonicalizing
@rust-bors

rust-bors Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 42e1aad (42e1aad072317b0ddfbfc70731cbb92bf50ea796)
Base parent: ba28ff7 (ba28ff76f353a722f31c4f3dd2ac4e437d36411b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (42e1aad): 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 @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.1% [0.1%, 0.1%] 3
Regressions ❌
(secondary)
0.1% [0.1%, 0.2%] 6
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 2
Improvements ✅
(secondary)
-1.4% [-3.4%, -0.4%] 22
All ❌✅ (primary) -0.0% [-0.2%, 0.1%] 5

Max RSS (memory usage)

Results (primary 6.2%)

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

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

Cycles

Results (primary -0.7%, 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.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
4.7% [2.8%, 6.7%] 9
Improvements ✅
(primary)
-2.1% [-2.1%, -2.0%] 2
Improvements ✅
(secondary)
-3.5% [-4.3%, -2.9%] 3
All ❌✅ (primary) -0.7% [-2.1%, 2.1%] 3

Binary size

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

Bootstrap: 457.997s -> 456.358s (-0.36%)
Artifact size: 396.43 MiB -> 397.06 MiB (0.16%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Aug 14, 2026
@nnethercote
nnethercote marked this pull request as ready for review August 14, 2026 02:57
@rustbot

rustbot commented Aug 14, 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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 14, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

LLM disclosure: the idea for this came from an LLM after analyzing a Cachegrind profile. I wrote the code and text myself.


fn release_canonicalizer_state(&self, mut state: CanonicalizerState<Self::Interner>) {
// Clear (don't deallocate) the state for later reuse.
state.clear();

@jdonszelmann jdonszelmann Aug 14, 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 be worth adding a debug assertion here that the cleared state == a default state. To make sure we never forget a field...

View changes since the review

pub fn clear(&mut self) {
// Deconstruct to ensure no fields are missed.
let Self { variables, var_kinds, variable_lookup_table, sub_root_lookup_table, cache } =
self;

@jdonszelmann jdonszelmann Aug 14, 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.

I guess deconstructing does help as well

View changes since the review

@jdonszelmann jdonszelmann left a comment

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.

Consider my comment, unsure whether its worth it, other than that I think this PR looks good to me

View changes since this review

@nnethercote

Copy link
Copy Markdown
Contributor Author

I think the deconstruction is good enough; it's a well-established idiom. Thanks!

@bors r=jdonszelmann

@rust-bors

rust-bors Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 445d379 has been approved by jdonszelmann

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added the merged-by-bors This PR was explicitly merged by bors. label Aug 14, 2026
@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 14, 2026
@rust-bors

rust-bors Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: jdonszelmann
Duration: 3h 22m 9s
Pushing d453bdd to main...

@rust-bors
rust-bors Bot merged commit d453bdd into rust-lang:main Aug 14, 2026
15 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 2fb4ed8 (parent) -> d453bdd (this PR)

Test differences

Show 2 test diffs

2 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard d453bdd8f092d099bc336f0bda4163f809ad18e0 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-gnu-debug: 1h 21m -> 1h 56m (+44.1%)
  2. dist-x86_64-netbsd: 59m 37s -> 1h 24m (+41.9%)
  3. x86_64-gnu-aux: 1h 53m -> 2h 37m (+39.3%)
  4. dist-x86_64-illumos: 1h 18m -> 1h 48m (+38.7%)
  5. aarch64-apple-1: 1h 33m -> 2h 5m (+35.0%)
  6. aarch64-apple-macos-26-2: 2h 31m -> 1h 40m (-34.1%)
  7. i686-gnu-2: 1h 37m -> 1h 5m (-32.9%)
  8. x86_64-gnu-next-trait-solver-polonius: 43m 49s -> 56m 25s (+28.7%)
  9. dist-x86_64-musl: 1h 44m -> 2h 14m (+28.6%)
  10. x86_64-gnu: 2h 37m -> 1h 53m (-28.2%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

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

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. 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