Skip to content

Shrink MIR basic-block statement vectors to fit before caching them - #163915

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
joshtriplett:mir-shrink-to-fit
Oct 9, 2026
Merged

rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
joshtriplett:mir-shrink-to-fit

Conversation

@joshtriplett

@joshtriplett joshtriplett commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

View all comments

Every MIR basic block allocates a vector for its statements. These statement vectors grow via the usual heuristics, and never get re-shrunk to the correct size for their contents. This holds on to a fair bit of memory.

Shrink them to fit right before we cache them.

For an aws-sdk-ec2 check, this saves ~80 MiB of peak max-RSS (~1.45%), at the cost of 0.3% in instructions.

r? nnethercote

Every MIR basic block allocates a vector for its statements. These
statement vectors grow via the usual heuristics, and never get re-shrunk
to the correct size for their contents. This holds on to a fair bit of
memory.

Shrink them to fit right before we cache them.

For an aws-sdk-ec2 check, this saves ~80 MiB of peak max-RSS (~1.45%),
at the cost of 0.3% in instructions.
@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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. labels Oct 7, 2026
@joshtriplett

Copy link
Copy Markdown
Member Author

I tested aws-sdk-ec2 because it's not in the perf suite, and I tested a few other things directly, but let's see the whole suite:

@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
Shrink MIR basic-block statement vectors to fit before caching them
@rust-bors

rust-bors Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 23ca711 (23ca711ad1b965faf6de390dc814f97c6c84da9e)
Base parent: 8d1a764 (8d1a76430406c877b35d0b627e7f796dcf0dfeca)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (23ca711): 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.2%] 22
Regressions ❌
(secondary)
0.2% [0.1%, 0.3%] 22
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) 0.1% [0.1%, 0.2%] 22

Max RSS (memory usage)

Results (primary -1.3%, secondary -2.0%)

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

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 1
Regressions ❌
(secondary)
2.9% [2.1%, 4.0%] 5
Improvements ✅
(primary)
-1.5% [-2.8%, -0.7%] 25
Improvements ✅
(secondary)
-3.5% [-7.5%, -0.7%] 16
All ❌✅ (primary) -1.3% [-2.8%, 2.2%] 26

Cycles

Results (primary -2.8%, secondary 5.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)
6.8% [3.4%, 16.1%] 7
Improvements ✅
(primary)
-2.8% [-2.8%, -2.8%] 2
Improvements ✅
(secondary)
-2.7% [-2.7%, -2.7%] 1
All ❌✅ (primary) -2.8% [-2.8%, -2.8%] 2

Binary size

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

Bootstrap: 489.893s -> 491.828s (0.39%)
Artifact size: 408.60 MiB -> 409.33 MiB (0.18%)

@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
@joshtriplett

Copy link
Copy Markdown
Member Author

The 0.1-0.2% performance regressions are expected, in exchange for the substantial max-RSS savings.

@joshtriplett

Copy link
Copy Markdown
Member Author

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Oct 7, 2026
@panstromek

panstromek commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

The 0.1-0.2% performance regressions are expected, in exchange for the substantial max-RSS savings.

I believe this is a bit of a problem, because people often make the tradeoff in the other direction, so somebody might just do the reverse optimization in the future. MaxRSS is also super noisy so it's a bit difficult to judge that tradeoff very well.

Nevertheless, I agree that we should prioritize memory a bit more. This has come up a few times recently (and on your Path PR as well), so I kicked off a thread on zulip about this: #t-compiler/performance > Prioritizing other metrics (Memory and Disk usage), I want to know what others think.

@@ -483,6 +483,10 @@ fn mir_promoted(
Some(MirPhase::Analysis(AnalysisPhase::Initial)),
);

@nnethercote nnethercote Oct 8, 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.

Can you add a short comment explaining that the small time cost is worth the memory saving? r=me with that

View changes since the review

@nnethercote nnethercote 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 8, 2026
@joshtriplett

Copy link
Copy Markdown
Member Author

@bors r=nnethercote

@rust-bors

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 1b7ba11 has been tentatively approved by nnethercote

It will be put into the queue for this repository once PR CI succeeds.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 9, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 9, 2026
Shrink MIR basic-block statement vectors to fit before caching them

Every MIR basic block allocates a vector for its statements. These statement vectors grow via the usual heuristics, and never get re-shrunk to the correct size for their contents. This holds on to a fair bit of memory.

Shrink them to fit right before we cache them.

For an aws-sdk-ec2 check, this saves ~80 MiB of peak max-RSS (~1.45%), at the cost of 0.3% in instructions.

r? nnethercote
@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors rollup=never rustc-perf
Bors didn't catch that command for some reason?

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-aarch64-msvc-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [run-make] tests\run-make\clear-error-blank-output stdout ----

error: rmake recipe failed to complete
status: exit code: 101
command: "C:\\a\\rust\\rust\\build\\aarch64-pc-windows-msvc\\test\\run-make\\clear-error-blank-output\\rmake.exe"
stdout: none
--- stderr -------------------------------

thread 'main' (7208) panicked at C:\a\rust\rust\tests\run-make\clear-error-blank-output\rmake.rs:11:64:
called `Result::unwrap()` on an `Err` value: Os { code: 232, kind: BrokenPipe, message: "The pipe is being closed." }
---
Currently active steps:
test::RunMake { test_compiler: Compiler { stage: 2, host: aarch64-pc-windows-msvc, forced_compiler: false }, target: aarch64-pc-windows-msvc } at src\bootstrap\src\core\build_steps\test.rs:2108
test::Compiletest { test_compiler: Compiler { stage: 2, host: aarch64-pc-windows-msvc, forced_compiler: false }, target: aarch64-pc-windows-msvc, mode: run-make, suite: "run-make", path: "tests/run-make", compare_mode: None } at src\bootstrap\src\core\build_steps\test.rs:2108
Build completed unsuccessfully in 1:51:53
make: *** [Makefile:115: ci-msvc-py] Error 1
  local time: Fri Oct  9 04:58:13 PDT 2026
  network time: Fri, 09 Oct 2026 11:58:13 GMT
##[error]Process completed with exit code 2.
##[group]Run echo "disk usage:"
echo "disk usage:"

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

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 9dbebb6 failed: CI. Failed job:

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors retry

@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 Oct 9, 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 Oct 9, 2026
@rust-bors

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: nnethercote
Duration: 3h 6m 49s
Pushing 69bccf0 to main...

@rust-bors
rust-bors Bot merged commit 69bccf0 into rust-lang:main Oct 9, 2026
15 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

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 76c9095 (parent) -> 69bccf0 (this PR)

Test differences

Show 3 test diffs

3 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 69bccf03c4733f57482f9691456efcd80f86a5f6 --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. test-x86_64-msvc-1: 1h 43m -> 2h 49m (+64.9%)
  2. dist-i686-linux: 1h 10m -> 1h 44m (+48.0%)
  3. test-x86_64-msvc-ext2: 1h 16m -> 1h 52m (+46.7%)
  4. dist-x86_64-netbsd: 1h 2m -> 1h 31m (+45.9%)
  5. test-x86_64-gnu-aux: 2h 42m -> 1h 29m (-44.9%)
  6. test-x86_64-gnu-stdlib-semver-check: 18m 16s -> 10m 31s (-42.4%)
  7. dist-android: 22m 19s -> 31m 47s (+42.4%)
  8. test-x86_64-gnu-stable: 1h 33m -> 2h 13m (+42.4%)
  9. test-aarch64-gnu: 2h 9m -> 1h 15m (-41.6%)
  10. test-pr-check-2: 40m 27s -> 23m 49s (-41.1%)
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 (69bccf0): 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.1% [0.1%, 0.2%] 6
Regressions ❌
(secondary)
0.1% [0.1%, 0.3%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 3
All ❌✅ (primary) 0.1% [0.1%, 0.2%] 6

Max RSS (memory usage)

Results (primary -1.3%, secondary -3.4%)

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

mean range count
Regressions ❌
(primary)
1.9% [1.9%, 1.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-2.2%, -0.9%] 12
Improvements ✅
(secondary)
-3.4% [-6.7%, -0.8%] 15
All ❌✅ (primary) -1.3% [-2.2%, 1.9%] 13

Cycles

Results (primary -2.8%)

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)
- - 0
Improvements ✅
(primary)
-2.8% [-2.8%, -2.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.8% [-2.8%, -2.8%] 1

Binary size

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

Bootstrap: 490.84s -> 487.936s (-0.59%)
Artifact size: 406.48 MiB -> 406.49 MiB (0.00%)

@joshtriplett
joshtriplett deleted the mir-shrink-to-fit branch October 9, 2026 17:41
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. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants