Skip to content

Commit e612e21

Browse files
committed
Reuse allocation for Vec<Candidate>
1 parent c036594 commit e612e21

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
5858
}
5959

6060
let ccx = ConstCx::new(tcx, body);
61-
let (mut temps, all_candidates) = collect_temps_and_candidates(&ccx);
61+
let (mut temps, mut all_candidates) = collect_temps_and_candidates(&ccx);
6262

63-
let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates);
63+
let promotable_candidates = validate_candidates(&ccx, &mut temps, &mut all_candidates);
6464

6565
let promoted = promote_candidates(body, tcx, temps, promotable_candidates);
6666
self.promoted_fragments.set(promoted);
@@ -688,18 +688,15 @@ impl<'tcx> Validator<'_, 'tcx> {
688688
}
689689
}
690690

691-
fn validate_candidates(
691+
fn validate_candidates<'a>(
692692
ccx: &ConstCx<'_, '_>,
693693
temps: &mut IndexSlice<Local, TempState>,
694-
candidates: &[Candidate],
695-
) -> Vec<Candidate> {
694+
candidates: &'a mut Vec<Candidate>,
695+
) -> &'a [Candidate] {
696696
let mut validator = Validator { ccx, temps, promotion_safe_blocks: None };
697697

698-
candidates
699-
.iter()
700-
.copied()
701-
.filter(|&candidate| validator.validate_candidate(candidate).is_ok())
702-
.collect()
698+
candidates.retain(|&candidate| validator.validate_candidate(candidate).is_ok());
699+
&candidates[..]
703700
}
704701

705702
struct Promoter<'a, 'tcx> {
@@ -965,7 +962,7 @@ fn promote_candidates<'tcx>(
965962
body: &mut Body<'tcx>,
966963
tcx: TyCtxt<'tcx>,
967964
mut temps: IndexVec<Local, TempState>,
968-
candidates: Vec<Candidate>,
965+
candidates: &[Candidate],
969966
) -> IndexVec<Promoted, Body<'tcx>> {
970967
// Visit candidates in reverse, in case they're nested.
971968
debug!(promote_candidates = ?candidates);
@@ -978,7 +975,7 @@ fn promote_candidates<'tcx>(
978975
let mut promotions = IndexVec::new();
979976

980977
let mut extra_statements = vec![];
981-
for candidate in candidates.into_iter().rev() {
978+
for candidate in candidates.iter().copied().rev() {
982979
let Location { block, statement_index } = candidate.location;
983980
if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind
984981
{

0 commit comments

Comments
 (0)