Skip to content

Avoid excessive memcpys with the new solver - #160005

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nnethercote:avoid-memcpy
Jul 28, 2026
Merged

Avoid excessive memcpys with the new solver#160005
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nnethercote:avoid-memcpy

Conversation

@nnethercote

@nnethercote nnethercote commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

View all comments

Cachegrind says that rustc with the new solver spends an enormous amount of time running memcpy when compiling nacl-0.5.3, ijson-0.1.6, and nvml-wrapper-sys-0.9.1. DHAT's copy mode says there are two places responsible for these excessive memcpys. This PR fixes them, for big performance wins on those crates. Details in the individual commits.

r? @lcnr

@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the core trait solver

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

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Jul 27, 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 Jul 27, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
Avoid excessive `memcpy`s with the new solver

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

can you add comments to the places using ThinVec to state that we do this for perf reasons for X crates and link to this PR?

View changes since this review

@rust-log-analyzer

This comment has been minimized.

@nnethercote

Copy link
Copy Markdown
Contributor Author

can you add comments to the places using ThinVec to state that we do this for perf reasons for X crates and link to this PR?

Done.

The perf run seems to be stuck. I re-pushed; let's try again.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 27, 2026
Avoid excessive `memcpy`s with the new solver
@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 3f08afa (3f08afab6dff94d9ad4eb9c29e0d71bcf620b626)
Base parent: dfbea5b (dfbea5bc945a9d7ff017d3d4bfbd1af9295fe5b8)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (3f08afa): 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
Regressions ❌
(secondary)
0.5% [0.3%, 0.8%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-4.8%, -0.2%] 13
All ❌✅ (primary) - - 0

Max RSS (memory usage)

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

Cycles

Results (secondary -0.2%)

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)
3.3% [2.3%, 4.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-3.1%, -2.1%] 3
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 486.74s -> 486.287s (-0.09%)
Artifact size: 387.74 MiB -> 387.63 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 Jul 27, 2026
@nnethercote

nnethercote commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Here are the local check full icount results for the new-solver benchmarks, including nacl-0.5.3, ijson-0.1.6, and nvml-wrapper-sys-0.9.1. cycles and wall-time results were similar (but noiser), e.g. around -40% for nacl and ijson.
image

Looks like the CI results aren't as good as the local results (e.g. -4.81% vs -7.31% for wg-grammar, -4.07% vs -5.77% for deeply-nested-multi, -2.03% vs -3.27% for serde). Probably a PGO thing. Still a very good result.

In `try_evaluate_obligations` we call `drain_pending` with a `|_, _|
true` closure. This closure is then used in a call to `partition`, which
is a very complicated way of moving all the elements into a new vector
that is identical to the old one. Each of those moves involves a
`memcpy` because the element type is large.

This commit changes the code to avoid the useless partition and just
take the vector directly, for a ~40% instruction count reduction when
doing `cargo check`.
`PendingObligations` is a `ThinVec` containing a pair
`(PredicateObligation<'tcx>, Option<GoalStalledOn<TyCtxt<'tcx>>>)`. This
pair is currently 136 bytes. This is more than 128 bytes, which means
that LLVM uses (slow) `memcpy` to move the elements.

This commit shrinks `GoalStalledOn` by changing its two `Vec` fields to
`ThinVec`. This shrinks the pair from 136 bytes to 104 bytes, under the
`memcpy` threshold.

The use of `ThinVec` in `GoalStalledOn` forces us to use `ThinVec` in a
number of other places. Some of these required the thin-vec `unstable`
feature for `may_dangle` support.

The commit also adds some size assertions, to prevent backsliding.
@rustbot

rustbot commented Jul 27, 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.

@nnethercote

nnethercote commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

I rebased over #159974, so this is now ready to land. Improvements greatly outweigh regressions.

@rustbot label: +perf-regression-triaged

@lcnr

lcnr commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 94a8720 has been approved by lcnr

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 Jul 28, 2026
@rust-bors

This comment has been minimized.

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

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: lcnr
Duration: 3h 10m 35s
Pushing e19d321 to main...

@rust-bors
rust-bors Bot merged commit e19d321 into rust-lang:main Jul 28, 2026
14 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 28, 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 e7b5955 (parent) -> e19d321 (this PR)

Test differences

Show 25 test diffs

25 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 e19d321c06479c6fd77533582b0d5a86651f1be3 --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-msvc-ext1: 1h 18m -> 2h 23m (+83.1%)
  2. x86_64-msvc-ext2: 1h 11m -> 1h 55m (+60.1%)
  3. x86_64-rust-for-linux: 36m 25s -> 49m 22s (+35.6%)
  4. x86_64-gnu-miri: 1h 31m -> 1h 2m (-31.8%)
  5. test-various: 1h 41m -> 2h 13m (+31.5%)
  6. dist-sparcv9-solaris: 1h 14m -> 1h 37m (+30.2%)
  7. i686-msvc-2: 2h 3m -> 1h 29m (-27.7%)
  8. dist-aarch64-apple: 1h 42m -> 2h 7m (+24.3%)
  9. x86_64-gnu-llvm-22-3: 1h 49m -> 1h 25m (-22.3%)
  10. dist-i686-mingw: 2h 48m -> 2h 10m (-22.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.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (e19d321): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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
Regressions ❌
(secondary)
0.4% [0.1%, 0.6%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.9% [-4.9%, -0.1%] 15
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -0.8%, secondary 4.1%)

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

mean range count
Regressions ❌
(primary)
1.2% [1.2%, 1.2%] 1
Regressions ❌
(secondary)
4.1% [2.2%, 6.0%] 2
Improvements ✅
(primary)
-2.7% [-2.7%, -2.7%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.8% [-2.7%, 1.2%] 2

Cycles

Results (primary -2.0%, secondary 1.2%)

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.1% [2.1%, 6.3%] 7
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
-2.9% [-3.5%, -2.4%] 5
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Binary size

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

Bootstrap: 490.22s -> 490.165s (-0.01%)
Artifact size: 390.35 MiB -> 388.34 MiB (-0.51%)

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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