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

use ManuallyDrop instead of forget inside collections #70766

Merged
merged 1 commit into from
Apr 5, 2020

Conversation

tspiteri
Copy link
Contributor

@tspiteri tspiteri commented Apr 4, 2020

This PR changes some usage of mem::forget into mem::ManuallyDrop in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was longer, and even after optimization, some unwinding artifacts were still present.

This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
@rust-highfive
Copy link
Collaborator

r? @cramertj

(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 4, 2020
@tspiteri
Copy link
Contributor Author

tspiteri commented Apr 4, 2020

About the IR, I tried with a simplified version of RawVec::into_box. Without optimization, the IR generated goes down from 466 lines to 131 lines, and the assembly code is shorter too [Compiler Explorer]. With optimization, the assembly is the same, but the IR is cleaner and loses some unwinding artifacts [Compiler Explorer].

@Centril
Copy link
Contributor

Centril commented Apr 4, 2020

r? @RalfJung

@rust-highfive rust-highfive assigned RalfJung and unassigned cramertj Apr 4, 2020
@@ -4,9 +4,10 @@ use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::iter::{FromIterator, FusedIterator, Peekable};
use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop};
Copy link
Member

Choose a reason for hiding this comment

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

Cc @Mark-Simulacrum @ssomers for BTreeMap changes

Copy link
Contributor

Choose a reason for hiding this comment

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

I hardly understand ManuallyDrop, but isn't it a good fit for btree::node::Root? I don't really understand Root's comment "Note that this does not have a destructor," to begin with. Doesn't Rust generate a destructor if you don't implement Drop? So the comment should say: "Note that we (hopefully) do not invoke the generated destructor, because we need to clean up the entire tree and we do that in a fancy and dangerous way while iterating".

Copy link
Member

Choose a reason for hiding this comment

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

I suppose that comment just means that there is no Drop impl that would clean up the data behind the raw pointers in Root -- usually one would expect such an impl to exist.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. But is that something more formally expressed by making Root.node ManuallyDrop? I should just try it and learn.

Copy link
Member

Choose a reason for hiding this comment

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

No, putting a raw pointer (or Unique) behind manually drop is pointless.

@@ -579,11 +578,10 @@ impl<T> RawVec<T, Global> {
"`len` must be smaller than or equal to `self.capacity()`"
);

let me = ManuallyDrop::new(self);
// NOTE: not calling `capacity()` here; actually using the real `cap` field!
Copy link
Member

Choose a reason for hiding this comment

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

Any idea what is up with that comment? cap is not used here, before not after your change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems that this happened recently in #70362, specifically in commit 2526acc.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like just an outdated comment then. Do you want to remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR is already r+-and-rollup-ed, so I'll remove the comment once the PR is merged to avoid merge issues.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I'll just add it to #70776 then

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the comment is outdated and I forgot to remove it.

@RalfJung
Copy link
Member

RalfJung commented Apr 4, 2020

I'm always in favor of replacing forget by ManuallyDrop. :D

This looks good to me, but I am not a libs expert, so I Cc'd some.

@Mark-Simulacrum
Copy link
Member

I would also be interested in an answer to #70766 (comment), but that's unrelated to this PR. @bors r=Mark-Simulacrum,RalfJung

@bors
Copy link
Contributor

bors commented Apr 4, 2020

📌 Commit 2b718e8 has been approved by Mark-Simulacrum,RalfJung

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 4, 2020
…Mark-Simulacrum,RalfJung

use ManuallyDrop instead of forget inside collections

This PR changes some usage of `mem::forget` into `mem::ManuallyDrop` in some `Vec`, `VecDeque`, `BTreeMap` and `Box` methods.

Before the commit, the generated IR for some of the methods was longer, and even after optimization, some unwinding artifacts were still present.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 5, 2020
Rollup of 6 pull requests

Successful merges:

 - rust-lang#70635 (rustc_target: Some cleanup to `no_default_libraries`)
 - rust-lang#70748 (Do not disable field reordering on enums with big discriminant)
 - rust-lang#70752 (Add slice::fill)
 - rust-lang#70766 (use ManuallyDrop instead of forget inside collections)
 - rust-lang#70768 (macro_rules: `NtLifetime` cannot start with an identifier)
 - rust-lang#70783 (comment refers to removed type)

Failed merges:

r? @ghost
@bors bors merged commit 590cb8b into rust-lang:master Apr 5, 2020
@tspiteri tspiteri deleted the forget-to-ManuallyDrop branch April 6, 2020 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants