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

Refactor unsafe implementation of std::mem::replace #117189

Closed
wants to merge 2 commits into from

Conversation

chfogelman
Copy link
Contributor

The function now defers to mem::swap, which both eliminates the use of unsafe in the function, and also allows it (and mem::take) to benefit from any performance enhancements to mem::swap, such as the current use of ptr::swap_nonoverlapping for large types.

The function now defers to mem::swap, which both
eliminates the use of `unsafe` in the function, and
also allows it (and mem::take) to benefit from
any performance enhancements to mem::swap, such as
the current use of ptr::swap_nonoverlapping for large
types.
@rustbot
Copy link
Collaborator

rustbot commented Oct 25, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 25, 2023
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Oct 25, 2023

The Miri subtree was changed

cc @rust-lang/miri

@chfogelman
Copy link
Contributor Author

Seems that there are more performance implications than I expected, as evidenced by the failure of tests/codegen/mem-replace-big-type.rs. Closing.

@chfogelman chfogelman closed this Oct 26, 2023
}
pub const fn replace<T>(dest: &mut T, mut src: T) -> T {
swap(dest, &mut src);
src
Copy link
Member

Choose a reason for hiding this comment

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

You were essentially trying to revert #83022 here.

Maybe we should have a comment explaining why this is not calling swap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think a comment is a good idea. It's not immediately clear that swap was being avoided for performance reasons.

Copy link
Member

Choose a reason for hiding this comment

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

Do you want to make a PR for that? :)

@chfogelman chfogelman deleted the replace-use-swap branch October 26, 2023 06:08
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 26, 2023
…t, r=thomcc

Explain implementation of mem::replace

This adds a comment to explain why `mem::replace` is not implemented in terms of `mem::swap` to prevent [naïfs like me](rust-lang#117189) from trying to "fix" it.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 27, 2023
Rollup merge of rust-lang#117243 - chfogelman:replace-not-swap-comment, r=thomcc

Explain implementation of mem::replace

This adds a comment to explain why `mem::replace` is not implemented in terms of `mem::swap` to prevent [naïfs like me](rust-lang#117189) from trying to "fix" it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants