about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorLukas Markeffsky <@>2024-10-28 22:59:14 +0100
committerLukas Markeffsky <@>2024-10-28 22:59:14 +0100
commit02ee6395fd4c7416691323415f575f65918e51f2 (patch)
treebafa69bb3b0db412d74f6b69346165e9724e8d52 /library/alloc/src
parent4a71a592819b6fe87377dd5f857218ed37dbf195 (diff)
Rc destructor: tweak inlining
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/rc.rs25
1 files changed, 17 insertions, 8 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs
index 52ec8237d83..fc8646e96d9 100644
--- a/library/alloc/src/rc.rs
+++ b/library/alloc/src/rc.rs
@@ -376,6 +376,21 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
     unsafe fn from_ptr_in(ptr: *mut RcInner<T>, alloc: A) -> Self {
         unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
     }
+
+    // Non-inlined part of `drop`.
+    #[inline(never)]
+    unsafe fn drop_slow(&mut self) {
+        // Reconstruct the "strong weak" pointer and drop it when this
+        // variable goes out of scope. This ensures that the memory is
+        // deallocated even if the destructor of `T` panics.
+        let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
+
+        // Destroy the contained object.
+        // We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
+        unsafe {
+            ptr::drop_in_place(&mut (*self.ptr.as_ptr()).value);
+        }
+    }
 }
 
 impl<T> Rc<T> {
@@ -2252,18 +2267,12 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
     /// 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 {
-                // Reconstruct the "strong weak" pointer and drop it when this
-                // variable goes out of scope. This ensures that the memory is
-                // deallocated even if the destructor of `T` panics.
-                let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
-
-                // Destroy the contained object.
-                // We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
-                ptr::drop_in_place(&mut (*self.ptr.as_ptr()).value);
+                self.drop_slow();
             }
         }
     }