Skip to content

Commit 9caf3bd

Browse files
committed
Give a bonus to Drop if the instantiation would be !needs_drop
1 parent 251a69c commit 9caf3bd

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const CONST_SWITCH_BONUS: usize = 10;
1212
const MULTIPLE_MUT_PENALTY: usize = 50;
1313
const REF_COPY_BONUS: usize = 5;
1414
const MANY_PARAMETERS_BONUS: usize = 10;
15+
const REMOVABLE_DROP_BONUS: usize = 20;
1516

1617
/// Verify that the callee body is compatible with the caller.
1718
#[derive(Clone)]
@@ -156,13 +157,16 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
156157
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
157158
match &terminator.kind {
158159
TerminatorKind::Drop { place, unwind, .. } => {
159-
// If the place doesn't actually need dropping, treat it like a regular goto.
160160
let ty = self.instantiate_ty(place.ty(self.callee_body, self.tcx).ty);
161161
if ty.needs_drop(self.tcx, self.param_env) {
162162
self.penalty += CALL_PENALTY;
163163
if let UnwindAction::Cleanup(_) = unwind {
164164
self.penalty += LANDINGPAD_PENALTY;
165165
}
166+
} else {
167+
// If the place doesn't actually need dropping,
168+
// we'd like to inline it to simplify the CFG
169+
self.bonus += REMOVABLE_DROP_BONUS;
166170
}
167171
}
168172
TerminatorKind::Call { func, unwind, .. } => {

0 commit comments

Comments
 (0)