Rc destructor: tweak inlining · qinheping/verify-rust-std@9318ae3 (original) (raw)

Original file line number Diff line number Diff line change
@@ -376,6 +376,21 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
376 376 unsafe fn from_ptr_in(ptr: *mut RcInner<T>, alloc: A) -> Self {
377 377 unsafe { Self::from_inner_in(NonNull::new_unchecked(ptr), alloc) }
378 378 }
379 +
380 +// Non-inlined part of `drop`.
381 +#[inline(never)]
382 +unsafe fn drop_slow(&mut self) {
383 +// Reconstruct the "strong weak" pointer and drop it when this
384 +// variable goes out of scope. This ensures that the memory is
385 +// deallocated even if the destructor of `T` panics.
386 +let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
387 +
388 +// Destroy the contained object.
389 +// We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
390 +unsafe {
391 + ptr::drop_in_place(&mut (*self.ptr.as_ptr()).value);
392 +}
393 +}
379 394 }
380 395
381 396 impl<T> Rc<T> {
@@ -2252,18 +2267,12 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
2252 2267 /// drop(foo); // Doesn't print anything
2253 2268 /// drop(foo2); // Prints "dropped!"
2254 2269 /// ```
2270 + #[inline]
2255 2271 fn drop(&mut self) {
2256 2272 unsafe {
2257 2273 self.inner().dec_strong();
2258 2274 if self.inner().strong() == 0 {
2259 -// Reconstruct the "strong weak" pointer and drop it when this
2260 -// variable goes out of scope. This ensures that the memory is
2261 -// deallocated even if the destructor of `T` panics.
2262 -let _weak = Weak { ptr: self.ptr, alloc: &self.alloc };
2263 -
2264 -// Destroy the contained object.
2265 -// We cannot use `get_mut_unchecked` here, because `self.alloc` is borrowed.
2266 - ptr::drop_in_place(&mut (*self.ptr.as_ptr()).value);
2275 +self.drop_slow();
2267 2276 }
2268 2277 }
2269 2278 }