-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1628,7 +1628,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> { | |
pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { | ||
// We are careful to *not* create a reference covering the "count" fields, as | ||
// this would conflict with accesses to the reference counts (e.g. by `Weak`). | ||
unsafe { &mut (*this.ptr.as_ptr()).value } | ||
unsafe { &mut *ptr::addr_of_mut!((*this.ptr.as_ptr()).value) } | ||
} | ||
|
||
#[inline] | ||
|
@@ -1885,10 +1885,10 @@ impl<T: ?Sized> Rc<T> { | |
// Initialize the RcBox | ||
let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr()); | ||
unsafe { | ||
debug_assert_eq!(Layout::for_value(&*inner), layout); | ||
debug_assert_eq!(Layout::for_value_raw(inner), layout); | ||
|
||
ptr::write(&mut (*inner).strong, Cell::new(1)); | ||
ptr::write(&mut (*inner).weak, Cell::new(1)); | ||
ptr::write(ptr::addr_of_mut!((*inner).strong), Cell::new(1)); | ||
ptr::write(ptr::addr_of_mut!((*inner).weak), Cell::new(1)); | ||
} | ||
|
||
Ok(inner) | ||
|
@@ -1902,7 +1902,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> { | |
// Allocate for the `RcBox<T>` using the given value. | ||
unsafe { | ||
Rc::<T>::allocate_for_layout( | ||
Layout::for_value(&*ptr), | ||
Layout::for_value_raw(ptr), | ||
|layout| alloc.allocate(layout), | ||
|mem| mem.with_metadata_of(ptr as *const RcBox<T>), | ||
) | ||
|
@@ -1918,7 +1918,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> { | |
// Copy value as bytes | ||
ptr::copy_nonoverlapping( | ||
&*src as *const T as *const u8, | ||
&mut (*ptr).value as *mut _ as *mut u8, | ||
ptr::addr_of_mut!((*ptr).value) as *mut u8, | ||
value_size, | ||
); | ||
|
||
|
@@ -1952,7 +1952,11 @@ impl<T> Rc<[T]> { | |
unsafe fn copy_from_slice(v: &[T]) -> Rc<[T]> { | ||
unsafe { | ||
let ptr = Self::allocate_for_slice(v.len()); | ||
ptr::copy_nonoverlapping(v.as_ptr(), &mut (*ptr).value as *mut [T] as *mut T, v.len()); | ||
ptr::copy_nonoverlapping( | ||
v.as_ptr(), | ||
ptr::addr_of_mut!((*ptr).value) as *mut T, | ||
v.len(), | ||
); | ||
Self::from_ptr(ptr) | ||
} | ||
} | ||
|
@@ -1987,15 +1991,15 @@ impl<T> Rc<[T]> { | |
let ptr = Self::allocate_for_slice(len); | ||
|
||
let mem = ptr as *mut _ as *mut u8; | ||
let layout = Layout::for_value(&*ptr); | ||
let layout = Layout::for_value_raw(ptr); | ||
|
||
// Pointer to first element | ||
let elems = &mut (*ptr).value as *mut [T] as *mut T; | ||
let elems = ptr::addr_of_mut!((*ptr).value) as *mut T; | ||
|
||
let mut guard = Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 }; | ||
|
||
for (i, item) in iter.enumerate() { | ||
ptr::write(elems.add(i), item); | ||
for item in iter { | ||
ptr::write(elems.add(guard.n_elems), item); | ||
Comment on lines
+2001
to
+2002
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive-by simplification |
||
guard.n_elems += 1; | ||
} | ||
|
||
|
@@ -2524,7 +2528,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for Rc<[T], A> { | |
let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc(); | ||
|
||
let rc_ptr = Self::allocate_for_slice_in(len, &alloc); | ||
ptr::copy_nonoverlapping(vec_ptr, &mut (*rc_ptr).value as *mut [T] as *mut T, len); | ||
ptr::copy_nonoverlapping(vec_ptr, ptr::addr_of_mut!((*rc_ptr).value) as *mut T, len); | ||
|
||
// Create a `Vec<T, &A>` with length 0, to deallocate the buffer | ||
// without dropping its contents or the allocator | ||
|
@@ -3099,7 +3103,10 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> { | |
// is dropped, the data field will be dropped in-place). | ||
Some(unsafe { | ||
let ptr = self.ptr.as_ptr(); | ||
WeakInner { strong: &(*ptr).strong, weak: &(*ptr).weak } | ||
WeakInner { | ||
strong: &*ptr::addr_of!((*ptr).strong), | ||
weak: &*ptr::addr_of!((*ptr).weak), | ||
} | ||
}) | ||
} | ||
} | ||
|
@@ -3499,7 +3506,9 @@ impl<T> DerefMut for UniqueRc<T> { | |
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we | ||
// have unique ownership and therefore it's safe to make a mutable reference because | ||
// `UniqueRc` owns the only strong reference to itself. | ||
unsafe { &mut (*self.ptr.as_ptr()).value } | ||
// We are careful to *not* create a reference covering the "count" fields, as | ||
// this would conflict with accesses to the weak reference count. | ||
unsafe { &mut *ptr::addr_of_mut!((*self.ptr.as_ptr()).value) } | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
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