rust icon indicating copy to clipboard operation
rust copied to clipboard

Merge RedundantImport into UnusedImport for suggesting removing spans

Open chenyukang opened this issue 1 year ago • 11 comments

Fixes #121315

I also changed the label of diagnostics for RedundantImport to be compatible with UnusedImport, or any other better ways?

r? @petrochenkov

chenyukang avatar Mar 08 '24 01:03 chenyukang

Let's run benchmarks first, to check whether it will perform as horribly as it looks, with all the repeated crate visiting and AST cloning. @bors try @rust-timer queue

petrochenkov avatar Mar 11 '24 16:03 petrochenkov

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

rust-timer avatar Mar 11 '24 16:03 rust-timer

:hourglass: Trying commit 5a1485b098f72699ffa080a1579274af99c4fbca with merge 000d1c672c221bf654ea63dea55353a4550e8539...

bors avatar Mar 11 '24 16:03 bors

:sunny: Try build successful - checks-actions Build commit: 000d1c672c221bf654ea63dea55353a4550e8539 (000d1c672c221bf654ea63dea55353a4550e8539)

bors avatar Mar 11 '24 18:03 bors

:sunny: Try build successful - checks-actions Build commit: 000d1c672c221bf654ea63dea55353a4550e8539 (000d1c672c221bf654ea63dea55353a4550e8539)

bors avatar Mar 11 '24 18:03 bors

Queued 000d1c672c221bf654ea63dea55353a4550e8539 with parent d255c6a57c393db6221b1ff700daea478436f1cd, future comparison URL. There is currently 1 preceding artifact in the queue. It will probably take at least ~2.2 hours until the benchmark run finishes.

rust-timer avatar Mar 11 '24 18:03 rust-timer

Finished benchmarking commit (000d1c672c221bf654ea63dea55353a4550e8539): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never @rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.0% [0.2%, 10.6%] 75
Regressions ❌
(secondary)
1.4% [0.7%, 5.5%] 14
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.0% [-0.5%, 10.6%] 76

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.8% [2.0%, 5.1%] 8
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
9.2% [1.6%, 30.8%] 39
Regressions ❌
(secondary)
3.4% [3.4%, 3.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.2% [-8.9%, -3.6%] 8
All ❌✅ (primary) 9.2% [1.6%, 30.8%] 39

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 647.596s -> 647.694s (0.02%) Artifact size: 310.01 MiB -> 309.93 MiB (-0.03%)

rust-timer avatar Mar 11 '24 20:03 rust-timer

https://github.com/rust-lang/rust/pull/122165#issuecomment-1989364344

Yep, pretty bad.

petrochenkov avatar Mar 11 '24 22:03 petrochenkov

In general, I'd like to see the whole unused import pass to be rewritten. The existing removal span collection logic (working on AST) was added without much consideration for the previous used-ness tracking logic (working on "lowered" imports). I'll try to look how it can be done better ~tomorrow.

petrochenkov avatar Mar 11 '24 22:03 petrochenkov

I was also worried about the performance, I thought the code change is not in the hot path, seems I'm wrong.

Yep, let's rethink it...

chenyukang avatar Mar 12 '24 01:03 chenyukang

So how I'd expect the check_unused pass to work at high level:

  • First we walk all lowered imports in the crate, using for module in self.arenas.local_modules().iter() { ... }, and collect a table T keyed on NodeIds and containing all the necessary usedness and redundantness info.
    • This walk and table makes Resolves::potentially_unused_imports and Resolver::used_imports fields unnecessary, they can be removed
  • Then we walk the whole crate once by a large visitor and process every use (or extern crate) item in it
    • Each use item is processed by a small visitor that collects spans of all unused/redundant imports in the item to build the removal multispan
    • AST of use items has NodeIds that we use to look into the table T to retrieve usedness/redundantness
    • This way we never have to clone pieces of AST

petrochenkov avatar Mar 12 '24 17:03 petrochenkov

@chenyukang any updates on this? thanks

Dylan-DPC avatar May 01 '24 12:05 Dylan-DPC

@chenyukang

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this. Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github. Thanks for your contribution.

@rustbot label: +S-inactive

oskgo avatar Jul 18 '24 21:07 oskgo