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

Optimize option clone #76552

Closed

Conversation

ridiculousfish
Copy link
Contributor

@ridiculousfish ridiculousfish commented Sep 10, 2020

These two commits improve the codegen of Option::clone.

The first commit "specializes" Option<T: Copy>::clone to a memcpy. For example, prior to this PR, cloning an Option<[u8; 8]>) branches on the Option's discriminant (link). Copying the bytes without a branch is more efficient.

The second commit adds the same optimization but specialized for ops::Range. Range<T: Copy> is famously not Copy (see #27186 and related); I do not intend to necro that discussion but I would like to see a more efficient clone(). Because Range is not Copy, this version uses unsafe ptr::read to achieve the same effect.

Note: the first commit has a user-visible behavior change in that Option<T>::clone() will no longer invoke T::clone() if T is Copy. This was considered as OK in RFC 1521 but it might be worth calling out in release notes that it now affects Option.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(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 Sep 10, 2020
@ridiculousfish
Copy link
Contributor Author

ridiculousfish commented Sep 10, 2020

I fully expect some pushback for the weird inclusion of Range inside Option, and am happy to pursue alternatives. I consider this PR as a jumping-off point for discussion for how to improve Option::clone's codegen, especially around Option<Range>.

@tesuji
Copy link
Contributor

tesuji commented Sep 10, 2020

cc @matthewjasper because this PR uses specialization.

Prior to this change, cloning an Option value would branch on whether the
Option was Some, even if it would be cheaper to just memcpy the bits.
Default the current implementation, and then specialize it for Copy types,
to avoid the branch.
Range<T> is not Copy even if T is Copy, which pessimizes the implementation
of Option<Range>::clone. Specialize Option::clone for Range so that it can
be more efficient. The specialization uses ptr::read to emulate Copy.
@cuviper
Copy link
Member

cuviper commented Sep 11, 2020

Usually we want specialization to go through a private trait, so the ability to specialize is not part of the public API.

@scottmcm
Copy link
Member

for the weird inclusion of Range inside Option

The thing that gets me here is that this also applies to every iterator -- a slice Iter also isn't copy, and could hypothetically benefit from this. So I'm not sure it makes sense to do it just for Range, but at the same time it also seems like overkill to do it for every single iterator adapter, for example.

--

A quick godbolt to show current codegen: https://rust.godbolt.org/z/YnG7rK

Brainstorming:

@jyn514 jyn514 added I-slow Issue: Problems and improvements with respect to performance of generated code. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 15, 2020
@pickfire
Copy link
Contributor

@ridiculousfish nice to see you trying out rust. Off-topic, we rewrote fish-update-completions in rust and it is almost done (logging and testing left).

Usually we want specialization to go through a private trait, so the ability to specialize is not part of the public API.

@ridiculousfish Try checking out the recent vec extend iter changes for specialization, this change may need to do something similar.

@crlf0710 crlf0710 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 Oct 8, 2020
@Dylan-DPC-zz
Copy link

@ridiculousfish any updates on this?

@ridiculousfish
Copy link
Contributor Author

Sorry, this presumably needs more work than I can take on at the moment. Closing so it's not in th eway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-slow Issue: Problems and improvements with respect to performance of generated code. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants