Skip to content

Lint to remove redundant clone()s #3355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 26, 2018
Merged

Conversation

sinkuu
Copy link
Contributor

@sinkuu sinkuu commented Oct 24, 2018

This PR adds lint redundant_clone. It suggests to remove redundant clone() that clones a owned value that will be dropped without any usage after that.

Real-world example: https://github.com/rust-lang/rust/compare/7b0735a..sinkuu:redundant_clone2

@flip1995
Copy link
Member

cc #17

@sinkuu sinkuu force-pushed the redundant_clone branch 2 times, most recently from a7433b0 to d3df19f Compare October 25, 2018 02:34
@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 25, 2018

cont_eval errors are apeearing in indexing_slicing.rs. This lint causes MIR to be optimized if it has't (since mir_validated is stolen sometimes, it uses optimized_mir instead), which may trigger const-evals. It should slowdown linting too.
EDIT: Compared cargo +nightly clippy and this branch, and the slowdown was around 10%.

How can I know if mir_validated is available? Checking for no_codegen || !should_codegen() didn't work...

@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 25, 2018

Ah, working on optimized MIR was much faster -- using mir_validated increased the slowdown to 50%, when it works. Perhaps because less number of BBs we need to visit? I'll just update indexing_slicing.stderr.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Wow! This is absolutely great.

One general nit: all these large if_lets and the bodies of filter_map would probably be more readable if they were separate functions with a descriptive name.

About these perf percentages that you posted, can you run clippy with and without your lint on some large code bases like cargo and diesel and compared those numbers? If it really causes a large slowdown, we can see how to optimize the lint to do less work

if let Some(snip) = snippet_opt(cx, span);
if let Some(dot) = snip.rfind('.');
then {
let sugg_span = span.with_lo(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm always wary of building our own spans, but I don't see how to do it better right now. Maybe we can change something in rustc that will provide us with better spans?


impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
fn visit_statement(&mut self, block: mir::BasicBlock, statement: &mir::Statement<'tcx>, location: mir::Location) {
// Once flagged, skip remaining statements
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be more performant to iterate manually and run the visitor directly on statements until we don't need it anymore

if ps.len() != 1 {
continue;
}
let pred_terminator = unwrap_or_continue!(&mir[ps[0]].terminator);
Copy link
Contributor

Choose a reason for hiding this comment

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

ah, this should never be None in MIR you get from the compiler. use the terminator method to get the value directly.

@Manishearth
Copy link
Member

Manishearth commented Oct 25, 2018 via email

@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 25, 2018

Result of hyperfine -p "touch path/to/lib.rs" -w 3 -i:

Package master [s] redundant_clone [s] Slowdown [%]
diesel 25.632 26.161 2.06
cargo 13.215 14.441 9.28
regex 1.983 2.176 9.73
serde-json 0.8877 0.9575 7.86

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

I don't really see where the slowdown is coming from. I presume it's a one-time cost we pay for computing the MIR of all function bodies (which we didn't so far I guess).

Can you confirm this by inserting a return right after you obtain the MIR and rerun the perf runs?

If that is the reason, then I'm absolutely fine with this slowdown.

};

if_chain! {
if !in_macro(span);
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this check all the way to the start of the loop and continue if we are in a macro.

@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 25, 2018

Quite noisy, but the slowdown caused by this lint may not be significant.

Package master [s] Only optimized_mir [s] Slowdown [%]
diesel 25.38 24.869 -2.01
cargo 12.897 14.467 12.17
regex 2.029 2.217 9.27
serde-json 0.7779 0.8556 9.99

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2018

bors r+

Awesome! Thanks so much for creating this lint. Both because it's awesome and because we now have a MIR lint

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2018

bors ping

@mati865
Copy link
Member

mati865 commented Oct 25, 2018

Maybe bors is mad at you?
I'm wondering if it will respond to unprivileged person.

bors ping

EDIT: it did!

@bors
Copy link
Contributor

bors bot commented Oct 25, 2018

pong

@flip1995
Copy link
Member

it was down, now it is back up again 🎉

bors r=oli-obk

bors bot added a commit that referenced this pull request Oct 25, 2018
3355: Lint to remove redundant `clone()`s r=oli-obk a=sinkuu

This PR adds lint `redundant_clone`. It suggests to remove redundant `clone()` that clones a owned value that will be dropped without any usage after that.

Real-world example: rust-lang/rust@8ec22e7...sinkuu:redundant_clone2

Co-authored-by: Shotaro Yamada <[email protected]>
@bors
Copy link
Contributor

bors bot commented Oct 25, 2018

Timed out

@notriddle
Copy link
Contributor

notriddle commented Oct 25, 2018

My bad. An upgrade went wrong, and caused some trouble over the last couple days.

... wait, I thought all the official Rust stuff used bors-voyager. What happened?

@Manishearth
Copy link
Member

That's just crates.io, the official rust stuff uses homu. I think we set up bors-ng because setting up homu requires some infra edits.

@flip1995
Copy link
Member

bors retry

bors bot added a commit that referenced this pull request Oct 26, 2018
3355: Lint to remove redundant `clone()`s r=oli-obk a=sinkuu

This PR adds lint `redundant_clone`. It suggests to remove redundant `clone()` that clones a owned value that will be dropped without any usage after that.

Real-world example: https://github.com/rust-lang/rust/compare/7b0735a..sinkuu:redundant_clone2

Co-authored-by: Shotaro Yamada <[email protected]>
@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 26, 2018

Nothing will pass until #3359 (comment) is resolved:(

@bors
Copy link
Contributor

bors bot commented Oct 26, 2018

Build failed

@matthiaskrgr
Copy link
Member

bors retry
Just needs a retry I think (not sure if I am privileged)

@bors
Copy link
Contributor

bors bot commented Oct 26, 2018

🔒 Permission denied

Existing reviewers: click here to make matthiaskrgr a reviewer

@flip1995
Copy link
Member

bors retry

@flip1995
Copy link
Member

bors ping

@bors
Copy link
Contributor

bors bot commented Oct 26, 2018

pong

1 similar comment
@bors
Copy link
Contributor

bors bot commented Oct 26, 2018

pong

@flip1995
Copy link
Member

It seems that if retry was written before another retry won't trigger bors again

bors r=oli-obk

bors bot added a commit that referenced this pull request Oct 26, 2018
3355: Lint to remove redundant `clone()`s r=oli-obk a=sinkuu

This PR adds lint `redundant_clone`. It suggests to remove redundant `clone()` that clones a owned value that will be dropped without any usage after that.

Real-world example: https://github.com/rust-lang/rust/compare/7b0735a..sinkuu:redundant_clone2

Co-authored-by: Shotaro Yamada <[email protected]>
@bors
Copy link
Contributor

bors bot commented Oct 26, 2018

@bors bors bot merged commit 9034b87 into rust-lang:master Oct 26, 2018
@sinkuu sinkuu deleted the redundant_clone branch October 30, 2018 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants