Skip to content

Inline Arc and Rc dealloc for T: !Drop #97676

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ impl<T: ?Sized> Rc<T> {
unsafe fn from_ptr(ptr: *mut RcBox<T>) -> Self {
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
}

// Non-inlined part of `drop`.
#[inline(never)]
unsafe fn drop_slow(&mut self) {
// Destroy the data at this time, even though we must not free the box
// allocation itself (there might still be weak pointers lying around).
unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };

// Drop the weak ref collectively held by all strong references.
drop(Weak { ptr: self.ptr });
}
}

impl<T> Rc<T> {
Expand Down Expand Up @@ -1516,20 +1527,19 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
/// drop(foo); // Doesn't print anything
/// drop(foo2); // Prints "dropped!"
/// ```
#[inline]
fn drop(&mut self) {
unsafe {
self.inner().dec_strong();
if self.inner().strong() == 0 {
// destroy the contained object
ptr::drop_in_place(Self::get_mut_unchecked(self));

// remove the implicit "strong weak" pointer now that we've
// destroyed the contents.
self.inner().dec_weak();
if self.inner().strong() != 0 {
return;
}

if self.inner().weak() == 0 {
Global.deallocate(self.ptr.cast(), Layout::for_value(self.ptr.as_ref()));
}
if mem::needs_drop::<T>() {
self.drop_slow();
} else {
// Drop the weak ref collectively held by all strong references.
drop(Weak { ptr: self.ptr });
}
}
}
Expand Down Expand Up @@ -2457,6 +2467,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Weak<T> {
///
/// assert!(other_weak_foo.upgrade().is_none());
/// ```
#[inline]
fn drop(&mut self) {
let inner = if let Some(inner) = self.inner() { inner } else { return };

Expand Down
10 changes: 8 additions & 2 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ impl<T: ?Sized> Arc<T> {
// allocation itself (there might still be weak pointers lying around).
unsafe { ptr::drop_in_place(Self::get_mut_unchecked(self)) };

// Drop the weak ref collectively held by all strong references
// Drop the weak ref collectively held by all strong references.
drop(Weak { ptr: self.ptr });
}

Expand Down Expand Up @@ -1699,7 +1699,12 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
acquire!(self.inner().strong);

unsafe {
self.drop_slow();
if mem::needs_drop::<T>() {
self.drop_slow();
} else {
// Drop the weak ref collectively held by all strong references.
drop(Weak { ptr: self.ptr });
}
}
}
}
Expand Down Expand Up @@ -2159,6 +2164,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Weak<T> {
///
/// assert!(other_weak_foo.upgrade().is_none());
/// ```
#[inline]
fn drop(&mut self) {
// If we find out that we were the last weak pointer, then its time to
// deallocate the data entirely. See the discussion in Arc::drop() about
Expand Down