Skip to content

perf: Avoid a heap allocation per basic block in MoveData's location maps - #160245

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
xmakro:perf/location-map-single-alloc
Aug 1, 2026
Merged

perf: Avoid a heap allocation per basic block in MoveData's location maps#160245
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
xmakro:perf/location-map-single-alloc

Conversation

@xmakro

@xmakro xmakro commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

MoveData holds two LocationMaps, and each was an IndexVec<BasicBlock, Vec<T>> that heap-allocated one Vec per basic block. Building the move data for a body allocated a small vector for every block, twice over. Store the entries in one flat buffer indexed by a per-block start offset, so each map is two allocations regardless of block count.

@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. labels Jul 31, 2026
@Kobzol

Kobzol commented Jul 31, 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 Jul 31, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 31, 2026
perf: Avoid a heap allocation per basic block in MoveData's location maps
@rust-bors

rust-bors Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a4ce053 (a4ce0538412cb5e1c770e8ba940d2922b1af928e)
Base parent: 8ab9fdf (8ab9fdff5a91b9f2b5ed57fb0275452d9a0d0280)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (a4ce053): comparison URL.

Overall result: ✅ improvements - no action needed

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.

@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
Improvements ✅
(primary)
-0.3% [-0.5%, -0.2%] 28
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.1%] 22
All ❌✅ (primary) -0.3% [-0.5%, -0.2%] 28

Max RSS (memory usage)

Results (primary -0.4%, 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)
0.5% [0.5%, 0.5%] 2
Regressions ❌
(secondary)
5.7% [5.7%, 5.7%] 1
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
-5.5% [-8.3%, -1.5%] 3
All ❌✅ (primary) -0.4% [-2.1%, 0.5%] 3

Cycles

Results (primary 0.4%, 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.8% [0.5%, 2.1%] 11
Regressions ❌
(secondary)
1.1% [0.5%, 3.1%] 10
Improvements ✅
(primary)
-2.0% [-2.6%, -1.4%] 2
Improvements ✅
(secondary)
-2.1% [-3.8%, -0.7%] 4
All ❌✅ (primary) 0.4% [-2.6%, 2.1%] 13

Binary size

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

Bootstrap: 492.09s -> 496.702s (0.94%)
Artifact size: 390.63 MiB -> 390.63 MiB (-0.00%)

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

xmakro commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

r? @nnethercote

@xmakro
xmakro marked this pull request as ready for review July 31, 2026 10:47
@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 Jul 31, 2026

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

Nice change! Just a few nits to fix.

View changes since this review

Comment thread compiler/rustc_mir_dataflow/src/move_paths/mod.rs Outdated
Comment thread compiler/rustc_mir_dataflow/src/move_paths/mod.rs Outdated
impl<T> IndexMut<Location> for LocationMap<T> {
fn index_mut(&mut self, index: Location) -> &mut Self::Output {
&mut self.map[index.block][index.statement_index]
let offset = self.offset(index);

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.

ditto

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.

unfortunately &mut self.data[self.offset(index)] fails borrowck:

error[E0502]: cannot borrow `*self` as immutable because it is also borrowed as mutable
    |
    |     fn index_mut(&mut self, index: Location) -> &mut Self::Output {
    |                  - let's call the lifetime of this reference `'1`
    |         &mut self.data[self.offset(index)]
    |         ---------------^^^^---------------
    |         |    |         |
    |         |    |         immutable borrow occurs here
    |         |    mutable borrow occurs here
    |         returning this value requires that `self.data` is borrowed for `'1`

@xmakro
xmakro force-pushed the perf/location-map-single-alloc branch from 57527a3 to b146323 Compare July 31, 2026 11:45
@xmakro

xmakro commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review!

@nnethercote

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b146323 has been approved by nnethercote

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 31, 2026
/// `block_starts[bb]` gives the index in `data` where block `bb`'s entries
/// start; each block has one entry per statement plus one for its terminator.
data: Vec<T>,
block_starts: IndexVec<BasicBlock, usize>,

@cjgillot cjgillot Jul 31, 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.

Should we eventually move to use an IndexVec<PointIndex> with a shared DenseLocationMap?

View changes since the review

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

rust-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: nnethercote
Duration: 3h 11m 2s
Pushing c47bb2f to main...

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

github-actions Bot commented Aug 1, 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 b6a3d79 (parent) -> c47bb2f (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 c47bb2f39658c00b109693df9611d7238bedf6fa --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: 2h 33m -> 1h 24m (-45.1%)
  2. arm-android: 1h 13m -> 1h 40m (+37.1%)
  3. x86_64-gnu-gcc-core-tests: 11m 38s -> 7m 20s (-36.9%)
  4. dist-loongarch64-linux: 1h 55m -> 1h 15m (-34.9%)
  5. dist-ohos-x86_64: 59m 4s -> 1h 17m (+31.0%)
  6. x86_64-mingw-1: 2h 19m -> 3h 1m (+29.6%)
  7. i686-gnu-nopt-2: 2h 21m -> 1h 40m (-29.1%)
  8. dist-riscv64-linux-gnu: 1h 7m -> 1h 26m (+29.0%)
  9. x86_64-gnu-parallel-frontend: 1h 32m -> 1h 52m (+22.4%)
  10. dist-android: 27m 20s -> 21m 25s (-21.7%)
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 (c47bb2f): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -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
Improvements ✅
(primary)
-0.3% [-0.5%, -0.2%] 23
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.1%] 21
All ❌✅ (primary) -0.3% [-0.5%, -0.2%] 23

Max RSS (memory usage)

Results (primary -4.2%, secondary -1.5%)

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)
-4.2% [-8.0%, -0.4%] 2
Improvements ✅
(secondary)
-1.5% [-4.6%, -0.4%] 7
All ❌✅ (primary) -4.2% [-8.0%, -0.4%] 2

Cycles

Results (primary -0.8%, 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)
- - 0
Regressions ❌
(secondary)
0.7% [0.6%, 1.0%] 3
Improvements ✅
(primary)
-0.8% [-1.8%, -0.4%] 7
Improvements ✅
(secondary)
-1.0% [-1.3%, -0.5%] 7
All ❌✅ (primary) -0.8% [-1.8%, -0.4%] 7

Binary size

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

Bootstrap: 491.498s -> 490.834s (-0.14%)
Artifact size: 390.44 MiB -> 390.52 MiB (0.02%)

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

6 participants