Skip to content

Perf: Improve looking up previously scanned items in dead-code - #166264

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
tehforsch:dead-code-perf
Nov 26, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
tehforsch:dead-code-perf

Conversation

@tehforsch

@tehforsch tehforsch commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Improve dead-code performance in looking up whether an item has already been scanned.

Before, we'd keep track of ScannedItem which is a combination of id, allow/expect and trivial usage site.

The idea here is to replace this, so that the scanned map stores the most important context from which we have scanned an item.
Without trivial usage sites, the scanned state of an item would form a total order, i.e. not scanned at all < scanned from allow expect < scanned normally. Essentially, the logic determining whether an item should be scanned can be reduced to the question of whether the seed we are coming from is "stronger" than the one from which we have previously scanned this item. This is nice because it means we can change the current set of ScannedItems to just a single map with one entry per DefId to store the most important context from which an item was scanned and be done, i.e.
LocalDefIdMap<ScannedFromSeed> where

enum ScannedFromSeed {
    Unscanned,
    AllowExpect,
    Live,
}

However, with trivial usage sites, there are many new possible scan states that a single item can have, since we need to additionally track the ScannedFromSeed for each usage site. Therefore, this PR effectively separates the scanned state into two maps:

scanned: LocalDefIdMap<ScannedFromSeed>,
// the keys are effectively (trivial_usage_site_id, node id)
scanned_trivial: FxHashMap<(LocalDefId, LocalDefId), ScannedFromSeed>,

Then, when we scan an item from a trivial usage site, use scanned_trivial, otherwise we use scanned.

In my benchmarks, this already helped with performance.As another optimization, using a IndexVec<LocalDefId, ScannedFromSeed> instead of a LocalDefIdMap made it quite a bit faster (-0.1% on libc) in my local perf runs . dead-code checks a lot of the DefIds in the crate, so a large portion of that vec ends up being full. Out of curiosity, I checked the fill rate of the vec and it varied a lot depending on the benchmark (unused-warnings has 100%, issue-58319 has only 1%), but for real world crates it seemed to be between 20% (rustc_borrowck) and 90% (libc) (very roughly). The only reason for a definition not to need an entry is if it is either unused or of a kind that we don't check, like generics, closures, ...

@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 Nov 20, 2026
@maxdexh

maxdexh commented Nov 20, 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 Nov 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Perf: Improve looking up previously scanned items in dead-code
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 400f915 (400f9154d38c0ffa8538a945ae1f7ece6ccdb071)
Base parent: 560abf7 (560abf7946ae41fd210edac5dfe829d7eff86eec)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (400f915): comparison URL.

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

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

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=iffy 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.3% [0.3%, 0.3%] 4
Regressions ❌
(secondary)
0.9% [0.9%, 0.9%] 6
Improvements ✅
(primary)
-0.2% [-0.3%, -0.1%] 16
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.1%] 18
All ❌✅ (primary) -0.1% [-0.3%, 0.3%] 20

Max RSS (memory usage)

Results (primary -2.2%, secondary 0.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)
0.7% [0.6%, 0.8%] 2
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.2% [-2.2%, -2.2%] 1

Cycles

Results (primary 4.8%, 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)
4.8% [4.8%, 4.8%] 1
Regressions ❌
(secondary)
0.9% [0.5%, 1.3%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.8% [4.8%, 4.8%] 1

Binary size

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

Bootstrap: 460.27s -> 461.022s (0.16%)
Artifact size: 405.34 MiB -> 405.37 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 20, 2026
@panstromek

Copy link
Copy Markdown
Contributor

The next-solver-stress regression is likely noise.

@tehforsch
tehforsch force-pushed the dead-code-perf branch 2 times, most recently from 2e572ac to 123031b Compare November 23, 2026 16:36
@Kobzol

Kobzol commented Nov 23, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 23, 2026
Perf: Improve looking up previously scanned items in dead-code
@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 Nov 23, 2026
@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 951098c (951098c4e1df2867ed98a9d252db572a5e656360)
Base parent: 960ba7c (960ba7cf7e8666ecb449402ca82b6e9e631bd227)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (951098c): 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.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.2% [-0.3%, -0.1%] 28
Improvements ✅
(secondary)
-0.5% [-1.2%, -0.1%] 24
All ❌✅ (primary) -0.2% [-0.3%, -0.1%] 28

Max RSS (memory usage)

Results (secondary -2.1%)

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)
5.8% [5.8%, 5.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-4.9%, -2.2%] 5
All ❌✅ (primary) - - 0

Cycles

Results (secondary 1.0%)

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)
5.6% [0.5%, 8.6%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-4.7%, -0.5%] 6
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 460.809s -> 460.985s (0.04%)
Artifact size: 406.40 MiB -> 405.77 MiB (-0.15%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 23, 2026
@tehforsch

Copy link
Copy Markdown
Contributor Author

r? @mu001999

@tehforsch
tehforsch marked this pull request as ready for review November 24, 2026 15:51
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 24, 2026

@mu001999 mu001999 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread compiler/rustc_passes/src/dead.rs Outdated
None => &mut self.scanned[id],
};

if *recorded >= Some(propagated) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a slight perf regression, I think this is probably because None will have the value 2 after niche opt, so that we didn't get an effective comparison.

Could you specify discriminants for ComesFromAllowExpect with Yes = 1 and No = 2? And then this line could be:

if recorded.map_or(0, |previous| previous as u8) >= propagated as u8 {
    return true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Using map_or and as u8 is because direct recorded >= Some(propagated) would emit a redundant instruction checks recorded == None (probably http://github.com/rust-lang/rust/issues/49892).

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.

Ok, done. Added a small comment about this too because it looks non-intuitive

@rustbot rustbot 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 Nov 26, 2026
@tehforsch

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Nov 26, 2026
@mu001999

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 Nov 26, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Perf: Improve looking up previously scanned items in dead-code
@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: c10bb28 (c10bb28ea81932f4a2ff8df0908d063af6a061dd)
Base parent: e068f42 (e068f42013e833ad8c4da25776b7fba798c2ea2d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (c10bb28): comparison URL.

Overall result: ✅ improvements - no action needed

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

@bors rollup=iffy 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.2% [-0.3%, -0.1%] 26
Improvements ✅
(secondary)
-0.4% [-0.6%, -0.2%] 15
All ❌✅ (primary) -0.2% [-0.3%, -0.1%] 26

Max RSS (memory usage)

Results (primary -1.6%, secondary -0.6%)

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.6% [0.4%, 0.9%] 6
Improvements ✅
(primary)
-1.6% [-1.6%, -1.6%] 1
Improvements ✅
(secondary)
-7.9% [-7.9%, -7.9%] 1
All ❌✅ (primary) -1.6% [-1.6%, -1.6%] 1

Cycles

Results (secondary 0.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)
1.7% [0.6%, 2.6%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.6% [-0.7%, -0.4%] 3
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 461.814s -> 469.914s (1.75%)
Artifact size: 405.58 MiB -> 405.63 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 26, 2026
@mu001999

Copy link
Copy Markdown
Member

Thanks!

@bors r+

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 572206b has been approved by mu001999

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 Nov 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 4 perf-sensitive pull requests

Successful merges:

 - #165489 (Add two extra early return checks to `is_recursive_terminator`)
 - #166248 (Make terminator not optional in BasicBlockData)
 - #166264 (Perf: Improve looking up previously scanned items in dead-code)
 - #166492 (rustc-lint: avoid allocation and Ident reconstruction in find_duplicated_parameters)
@rust-bors
rust-bors Bot merged commit 0c16a8f into rust-lang:main Nov 26, 2026
15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Rollup merge of #166264 - tehforsch:dead-code-perf, r=mu001999

Perf: Improve looking up previously scanned items in dead-code

Improve `dead-code` performance in looking up whether an item has already been scanned.

Before, we'd keep track of `ScannedItem` which is a combination of `id`, allow/expect and trivial usage site.

The idea here is to replace this, so that the `scanned` map stores the most important context from which we have scanned an item.
Without trivial usage sites, the scanned state of an item would form a total order, i.e. `not scanned at all < scanned from allow expect < scanned normally`. Essentially, the logic determining whether an item should be scanned can be reduced to the question of whether the seed we are coming from is "stronger" than the one from which we have previously scanned this item. This is nice because it means we can change the current set of `ScannedItems` to just a single map with one entry per `DefId` to store the most important context from which an item was scanned and be done, i.e.
`LocalDefIdMap<ScannedFromSeed>` where
```rust
enum ScannedFromSeed {
    Unscanned,
    AllowExpect,
    Live,
}
```

However, with trivial usage sites, there are many new possible scan states that a single item can have, since we need to additionally track the `ScannedFromSeed` for each usage site. Therefore, this PR effectively separates the `scanned` state into two maps:
```rust
scanned: LocalDefIdMap<ScannedFromSeed>,
// the keys are effectively (trivial_usage_site_id, node id)
scanned_trivial: FxHashMap<(LocalDefId, LocalDefId), ScannedFromSeed>,
```

Then, when we scan an item from a trivial usage site, use `scanned_trivial`, otherwise  we use `scanned`.

In my benchmarks, this already helped with performance.As another optimization, using a `IndexVec<LocalDefId, ScannedFromSeed>` instead of a `LocalDefIdMap` made it quite a bit faster (-0.1% on `libc`) in my local perf runs . `dead-code` checks a lot of the `DefId`s in the crate, so a large portion of that vec ends up being full. Out of curiosity, I checked the fill rate of the vec and it varied a lot depending on the benchmark (`unused-warnings` has 100%, `issue-58319` has only 1%), but for real world crates it seemed to be between 20% (`rustc_borrowck`) and 90% (`libc`) (very roughly). The only reason for a definition not to need an entry is if it is either unused or of a kind that we don't check, like generics, closures, ...
@rust-timer

Copy link
Copy Markdown

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (0ba1f5b): 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.2% [-0.3%, -0.1%] 19
Improvements ✅
(secondary)
-0.5% [-0.7%, -0.2%] 11
All ❌✅ (primary) -0.2% [-0.3%, -0.1%] 19

Max RSS (memory usage)

Results (primary -1.6%)

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

Cycles

Results (primary -2.4%, secondary -3.0%)

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.4% [-3.1%, -1.7%] 9
Improvements ✅
(secondary)
-3.0% [-3.9%, -2.5%] 3
All ❌✅ (primary) -2.4% [-3.1%, -1.7%] 9

Binary size

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

Artifact size: 405.62 MiB -> 405.70 MiB (0.02%)

mehmettoplu-rs pushed a commit to mehmettoplu-rs/miri that referenced this pull request Nov 27, 2026
…uwer

Rollup of 4 perf-sensitive pull requests

Successful merges:

 - rust-lang/rust#165489 (Add two extra early return checks to `is_recursive_terminator`)
 - rust-lang/rust#166248 (Make terminator not optional in BasicBlockData)
 - rust-lang/rust#166264 (Perf: Improve looking up previously scanned items in dead-code)
 - rust-lang/rust#166492 (rustc-lint: avoid allocation and Ident reconstruction in find_duplicated_parameters)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 28, 2026
…ShoyuVanilla

Call `check_def_id` when meeting assoc consts

I noticed this when reviewing rust-lang#166264, but this is not related to that PR :).

`WorkItem` shouldn't be constructed manually, excluding `create_and_seed_worklist` and `check_def_id`. `check_def_id` will handle the `allow/expect` propagation correctly.
rust-bors Bot pushed a commit that referenced this pull request Nov 28, 2026
Rollup merge of #166565 - mu001999-contrib:fix/dead-code, r=ShoyuVanilla

Call `check_def_id` when meeting assoc consts

I noticed this when reviewing #166264, but this is not related to that PR :).

`WorkItem` shouldn't be constructed manually, excluding `create_and_seed_worklist` and `check_def_id`. `check_def_id` will handle the `allow/expect` propagation correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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