Skip to content

Commit 3ecb0bc

Browse files
committed
Polymorph the Box's drop as well
Doing this for allocating them was a nice perf win in 152737, so might help here too.
1 parent 9cabb08 commit 3ecb0bc

8 files changed

Lines changed: 121 additions & 378 deletions

library/alloc/src/boxed.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,12 +1923,27 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
19231923
fn drop(&mut self) {
19241924
// the T in the Box is dropped by the compiler before the destructor is run
19251925

1926-
let ptr = self.0;
1927-
1926+
let ptr = Box::as_ptr(self);
1927+
// SAFETY: By type invariants, this is the allocator and layout for the pointer.
19281928
unsafe {
1929-
let layout = Layout::for_value_raw(ptr.as_ptr());
1930-
if layout.size() != 0 {
1931-
self.1.deallocate(From::from(ptr.cast()), layout);
1929+
box_drop_inner(&self.1, ptr as *const u8, Layout::for_value_raw(ptr));
1930+
}
1931+
1932+
/// # Safety
1933+
/// If `layout` is non-ZST, then `ptr` must have been allocated
1934+
/// with compatible `layout` by `a`.
1935+
/// (or `layout` is a ZST)
1936+
// This is intentionally polymorphized, like `box_new_uninit`, so we
1937+
// keep it from inlining in MIR, and leave it up to the backend to
1938+
// decide whether it's actually worth inlining.
1939+
#[rustc_no_mir_inline]
1940+
#[inline]
1941+
unsafe fn box_drop_inner(a: &impl Allocator, ptr: *const u8, layout: Layout) {
1942+
// SAFETY: Obligations propagated to the caller
1943+
unsafe {
1944+
if layout.size() != 0 {
1945+
a.deallocate(NonNull::new_unchecked(ptr.cast_mut()), layout);
1946+
}
19321947
}
19331948
}
19341949
}

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.32bit.panic-abort.mir

Lines changed: 0 additions & 92 deletions
This file was deleted.

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.32bit.panic-unwind.mir

Lines changed: 0 additions & 92 deletions
This file was deleted.

tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.64bit.panic-abort.mir

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)