Skip to content

Commit eabe4fc

Browse files
authored
Add drop_slow to possibly avoid inlining type T drop based on compiler decision
1 parent 5ea7386 commit eabe4fc

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/arc.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ impl<T> Arc<T> {
390390
pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {
391391
unsafe { &mut *(*this.ptr.as_ptr()).data.get() }
392392
}
393+
394+
// Non-inlined part of `drop`. Just invokes the destructor.
395+
unsafe fn drop_slow(&mut self) {
396+
let _ = Box::from_raw(self.ptr.as_ptr());
397+
}
393398
}
394399

395400
impl<T: Clone> Arc<T> {
@@ -550,9 +555,11 @@ impl<T> Drop for Arc<T> {
550555
if self.inner().counter.fetch_sub(1, Ordering::Release) != 1 {
551556
return;
552557
}
558+
553559
fence(Ordering::Acquire);
560+
554561
// SAFETY: this is the last owner of the ptr, it is safe to drop data
555-
unsafe { Box::from_raw(self.ptr.as_ptr()) };
562+
unsafe { self.drop_slow() };
556563
}
557564
}
558565

src/rc.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ impl<T> Rc<T> {
311311
Err(this)
312312
}
313313
}
314+
315+
// Non-inlined part of `drop`. Just invokes the destructor.
316+
unsafe fn drop_slow(&mut self) {
317+
let _ = Box::from_raw(self.ptr.as_mut());
318+
}
314319
}
315320

316321
impl<T: Clone> Rc<T> {
@@ -470,7 +475,7 @@ impl<T> Drop for Rc<T> {
470475
let value = value.wrapping_sub(1);
471476
counter.set(value);
472477
} else {
473-
unsafe { Box::from_raw(self.ptr.as_mut()) };
478+
unsafe { self.drop_slow() };
474479
}
475480
}
476481
}

0 commit comments

Comments
 (0)