Skip to content
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

Specialize derive(Clone) to use Copy when applicable #95668

Closed
wants to merge 4 commits into from

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Apr 5, 2022

In this PR I added two internal items in core::clone: try_copy and DerivedClone.

try_copy will copy from &T to T if it can, and call a function when it cannot.

The derive impl for Clone is changed such that it generates a call to try_copy(self, DerivedClone::clone), and the impl for DerivedClone contains the derive impl before this PR.

Internally:
I added a field to TraitDef named bound_current_trait that disables addition of generic bounds of the trait that is being implemented. This prevents bounding on DerivedClone when we are generating that impl.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2022
@rust-highfive
Copy link
Collaborator

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 5, 2022
@fee1-dead
Copy link
Member Author

Could be perf sensitive, so:

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

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

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 6, 2022
@bors
Copy link
Contributor

bors commented Apr 6, 2022

⌛ Trying commit 95183ec with merge 074fe5f224a2ee59233947b06ddfd86252e1086c...

@bors
Copy link
Contributor

bors commented Apr 6, 2022

☀️ Try build successful - checks-actions
Build commit: 074fe5f224a2ee59233947b06ddfd86252e1086c (074fe5f224a2ee59233947b06ddfd86252e1086c)

@rust-timer
Copy link
Collaborator

Queued 074fe5f224a2ee59233947b06ddfd86252e1086c with parent 306ba83, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (074fe5f224a2ee59233947b06ddfd86252e1086c): comparison url.

Summary:

  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
Regressions 😿
(primary)
Regressions 😿
(secondary)
Improvements 🎉
(primary)
Improvements 🎉
(secondary)
All 😿 🎉
(primary)
count1 114 65 10 22 124
mean2 1.1% 3.7% -0.7% -0.5% 0.9%
max 3.2% 9.2% -1.3% -1.0% 3.2%

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

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 led 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-review -S-waiting-on-perf +perf-regression

Footnotes

  1. number of relevant changes

  2. the arithmetic mean of the percent change

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 6, 2022
@jackh726
Copy link
Member

Do you need me to review this prior to fixing perf?

@fee1-dead
Copy link
Member Author

Yes, I think perf might be unavoidable.

@jackh726
Copy link
Member

What's the point of this, if not perf?

@fee1-dead
Copy link
Member Author

compiler perf will definitely be negatively affected since we now emit two items for the derive. The question would be, "are there people who implement Copy manually or derive(Copy) in a different attribute while deriving clone?"

If there is a significant amount of them, then I think this would definitely be beneficial. But now it doesn't seem to have much of a benefit. Maybe we could discuss the possibility of doing something like this with perfect derives.

@fee1-dead fee1-dead closed this Apr 23, 2022
@scottmcm
Copy link
Member

Note that in the case of something like

#[derive(Clone)]
pub struct Foo(usize, HashSet<String>, usize);

The mir-opt in #94276 will replace the clone calls on the primitives with copies pretty early, to simplify the MIR.

So that along with LLVM being pretty good at inlining the clones enough to notice they're actually copies (even the MIR inliner can often do this, though it's not on yet), it's possible that most of the low-hanging fruit here has already been picked.

@fee1-dead fee1-dead deleted the always-use-copy branch April 19, 2023 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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