-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
alloc: avoid creating references in implementation of Rc
/Arc
#119691
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
for item in iter { | ||
ptr::write(elems.add(guard.n_elems), item); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by simplification
I believe this is a duplicate of #119433. |
whoops, I missed that while searching for existing PRs :/ |
unsafe { &mut (*this.ptr.as_ptr()).value } | ||
unsafe { &mut *ptr::addr_of_mut!((*this.ptr.as_ptr()).value) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this created any unnecessary references, the addr_of_mut
roundtrip seems pointless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you're right, but this does result in different MIR output.
I thought that they were different because the previous line created a reference to the entire struct, not just that one field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that they were different because the previous line created a reference to the entire struct, not just that one field
IIUC, &mut (*ptr).field
does not create a reference to the entire struct. See also the following lines in Weak::inner.
rust/library/alloc/src/sync.rs
Lines 2849 to 2852 in 87e1430
// We are careful to *not* create a reference covering the "data" field, as | |
// the field may be mutated concurrently (for example, if the last `Arc` | |
// is dropped, the data field will be dropped in-place). | |
Some(unsafe { WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak } }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that comment but I didn't quite trust it because it's apparently part of faulty code :) I guess I can close this tho
IIUC, most of the differences appeared to be actually unnecessary changes. Like #119691 (comment). |
I noticed that the implementations of
Rc
/Arc
create a lot of intermediate references to values that are not initialised or that shouldn't be accessed because of aliasing rules. This PR replaces some dubious cases withptr::addr_of(_mut)
and replacesLayout::for_value
withLayout::for_value_raw
.