Repository navigation
Perf: Improve looking up previously scanned items in dead-code - #166264
Conversation
cb70b31 to
34cdb0c
Compare
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Perf: Improve looking up previously scanned items in dead-code
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=iffy rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary 4.8%, secondary 0.9%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 460.27s -> 461.022s (0.16%) |
bf50708 to
34cdb0c
Compare
|
The |
2e572ac to
123031b
Compare
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Perf: Improve looking up previously scanned items in dead-code
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (secondary 1.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 460.809s -> 460.985s (0.04%) |
|
r? @mu001999 |
| None => &mut self.scanned[id], | ||
| }; | ||
|
|
||
| if *recorded >= Some(propagated) { |
There was a problem hiding this comment.
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;
}There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Ok, done. Added a small comment about this too because it looks non-intuitive
86047fc to
572206b
Compare
|
@rustbot ready |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Perf: Improve looking up previously scanned items in dead-code
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (c10bb28): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking 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 Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (secondary 0.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 461.814s -> 469.914s (1.75%) |
|
Thanks! @bors r+ |
…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)
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, ...
|
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 countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -2.4%, secondary -3.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Artifact size: 405.62 MiB -> 405.70 MiB (0.02%) |
…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)
…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.
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.
View all comments
Improve
dead-codeperformance in looking up whether an item has already been scanned.Before, we'd keep track of
ScannedItemwhich is a combination ofid, allow/expect and trivial usage site.The idea here is to replace this, so that the
scannedmap 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 ofScannedItemsto just a single map with one entry perDefIdto store the most important context from which an item was scanned and be done, i.e.LocalDefIdMap<ScannedFromSeed>whereHowever, with trivial usage sites, there are many new possible scan states that a single item can have, since we need to additionally track the
ScannedFromSeedfor each usage site. Therefore, this PR effectively separates thescannedstate into two maps:Then, when we scan an item from a trivial usage site, use
scanned_trivial, otherwise we usescanned.In my benchmarks, this already helped with performance.As another optimization, using a
IndexVec<LocalDefId, ScannedFromSeed>instead of aLocalDefIdMapmade it quite a bit faster (-0.1% onlibc) in my local perf runs .dead-codechecks a lot of theDefIds 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-warningshas 100%,issue-58319has 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, ...