Skip to content

Commit fab18b2

Browse files
committed
Don't instantiate so many copies of real_drop_in_place
1 parent 0e6ccff commit fab18b2

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/librustc/ty/instance.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ pub enum InstanceDef<'tcx> {
5252
/// `<[mut closure] as FnOnce>::call_once`
5353
ClosureOnceShim { call_once: DefId },
5454

55-
/// `drop_in_place::<T>; None` for empty drop glue.
55+
/// `real_drop_in_place::<T>`; `None` for empty drop glue. The `DefId` is
56+
/// for `real_drop_in_place`.
5657
DropGlue(DefId, Option<Ty<'tcx>>),
5758

5859
///`<T as Clone>::clone` shim.
@@ -108,11 +109,28 @@ impl<'tcx> InstanceDef<'tcx> {
108109
if self.is_inline(tcx) {
109110
return true
110111
}
111-
if let ty::InstanceDef::DropGlue(..) = *self {
112-
// Drop glue wants to be instantiated at every codegen
112+
if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self {
113+
// Drop glue generally wants to be instantiated at every codegen
113114
// unit, but without an #[inline] hint. We should make this
114115
// available to normal end-users.
115-
return true
116+
if tcx.sess.opts.incremental.is_none() {
117+
return true;
118+
}
119+
// When compiling with incremental, we can generate a *lot* of
120+
// codegen units. Including drop glue into all of them has a
121+
// considerable compile time cost.
122+
//
123+
// We include enums without destructors to allow, say, optimizing
124+
// drops of `Option::None` before LTO. We also respect the intent of
125+
// `#[inline]` on `Drop::drop` implementations.
126+
return ty.ty_adt_def()
127+
.map_or(true, |adt_def| {
128+
adt_def.destructor(tcx)
129+
.map_or(
130+
adt_def.is_enum(),
131+
|dtor| tcx.codegen_fn_attrs(dtor.did).requests_inline(),
132+
)
133+
})
116134
}
117135
tcx.codegen_fn_attrs(self.def_id()).requests_inline()
118136
}

src/librustc_mir/monomorphize/partitioning.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -690,13 +690,20 @@ fn characteristic_def_id_of_mono_item<'tcx>(
690690

691691
if tcx.trait_of_item(def_id).is_some() {
692692
let self_ty = instance.substs.type_at(0);
693-
// This is an implementation of a trait method.
693+
// This is a default implementation of a trait method.
694694
return characteristic_def_id_of_type(self_ty).or(Some(def_id));
695695
}
696696

697697
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
698-
// This is a method within an inherent impl, find out what the
699-
// self-type is:
698+
if tcx.sess.opts.incremental.is_some()
699+
&& tcx.trait_id_of_impl(impl_def_id) == tcx.lang_items().drop_trait()
700+
{
701+
// Put `Drop::drop` into the same cgu as `real_drop_in_place`
702+
// since `real_drop_in_place` is the only thing that can
703+
// call it.
704+
return None;
705+
}
706+
// This is a method within an impl, find out what the self-type is:
700707
let impl_self_ty = tcx.subst_and_normalize_erasing_regions(
701708
instance.substs,
702709
ty::ParamEnv::reveal_all(),

0 commit comments

Comments
 (0)