Skip to content

Commit 9cf6ce0

Browse files
committed
Remove more PlaceBuilder clones
1 parent 105abe3 commit 9cf6ce0

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

compiler/rustc_mir_build/src/build/expr/as_place.rs

+8
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ impl<'tcx> PlaceBuilder<'tcx> {
315315
self.projection.push(elem);
316316
self
317317
}
318+
319+
/// Same as `.clone().project(..)` but more efficient
320+
pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
321+
Self {
322+
base: self.base,
323+
projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
324+
}
325+
}
318326
}
319327

320328
impl<'tcx> From<Local> for PlaceBuilder<'tcx> {

compiler/rustc_mir_build/src/build/expr/into.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
363363
.map(|(n, ty)| match fields_map.get(&n) {
364364
Some(v) => v.clone(),
365365
None => {
366-
let place_builder = place_builder.clone();
367-
this.consume_by_copy_or_move(
368-
place_builder.field(n, *ty).to_place(this),
369-
)
366+
let place = place_builder.clone_project(PlaceElem::Field(n, *ty));
367+
this.consume_by_copy_or_move(place.to_place(this))
370368
}
371369
})
372370
.collect()

compiler/rustc_mir_build/src/build/matches/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
168168
let scrutinee_place =
169169
unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,));
170170

171-
let mut arm_candidates = self.create_match_candidates(scrutinee_place.clone(), &arms);
171+
let mut arm_candidates = self.create_match_candidates(&scrutinee_place, &arms);
172172

173173
let match_has_guard = arm_candidates.iter().any(|(_, candidate)| candidate.has_guard);
174174
let mut candidates =
@@ -230,7 +230,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
230230
/// Create the initial `Candidate`s for a `match` expression.
231231
fn create_match_candidates<'pat>(
232232
&mut self,
233-
scrutinee: PlaceBuilder<'tcx>,
233+
scrutinee: &PlaceBuilder<'tcx>,
234234
arms: &'pat [ArmId],
235235
) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)>
236236
where
@@ -1332,15 +1332,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13321332
bug!("Or-patterns should have been sorted to the end");
13331333
};
13341334
let or_span = match_pair.pattern.span;
1335-
let place = match_pair.place;
13361335

13371336
first_candidate.visit_leaves(|leaf_candidate| {
13381337
self.test_or_pattern(
13391338
leaf_candidate,
13401339
&mut otherwise,
13411340
pats,
13421341
or_span,
1343-
place.clone(),
1342+
&match_pair.place,
13441343
fake_borrows,
13451344
);
13461345
});
@@ -1368,7 +1367,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13681367
otherwise: &mut Option<BasicBlock>,
13691368
pats: &'pat [Box<Pat<'tcx>>],
13701369
or_span: Span,
1371-
place: PlaceBuilder<'tcx>,
1370+
place: &PlaceBuilder<'tcx>,
13721371
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
13731372
) {
13741373
debug!("candidate={:#?}\npats={:#?}", candidate, pats);
@@ -1607,7 +1606,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16071606
// encounter a candidate where the test is not relevant; at
16081607
// that point, we stop sorting.
16091608
while let Some(candidate) = candidates.first_mut() {
1610-
let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else {
1609+
let Some(idx) = self.sort_candidate(&match_place, &test, candidate) else {
16111610
break;
16121611
};
16131612
let (candidate, rest) = candidates.split_first_mut().unwrap();
@@ -1676,7 +1675,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16761675
target_blocks
16771676
};
16781677

1679-
self.perform_test(span, scrutinee_span, block, match_place, &test, make_target_blocks);
1678+
self.perform_test(span, scrutinee_span, block, &match_place, &test, make_target_blocks);
16801679
}
16811680

16821681
/// Determine the fake borrows that are needed from a set of places that

compiler/rustc_mir_build/src/build/matches/simplify.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7373
{
7474
existing_bindings.extend_from_slice(&new_bindings);
7575
mem::swap(&mut candidate.bindings, &mut existing_bindings);
76-
candidate.subcandidates =
77-
self.create_or_subcandidates(candidate, place.clone(), pats);
76+
candidate.subcandidates = self.create_or_subcandidates(candidate, &place, pats);
7877
return true;
7978
}
8079

@@ -127,7 +126,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
127126
fn create_or_subcandidates<'pat>(
128127
&mut self,
129128
candidate: &Candidate<'pat, 'tcx>,
130-
place: PlaceBuilder<'tcx>,
129+
place: &PlaceBuilder<'tcx>,
131130
pats: &'pat [Box<Pat<'tcx>>],
132131
) -> Vec<Candidate<'pat, 'tcx>> {
133132
pats.iter()

compiler/rustc_mir_build/src/build/matches/test.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
150150
match_start_span: Span,
151151
scrutinee_span: Span,
152152
block: BasicBlock,
153-
place_builder: PlaceBuilder<'tcx>,
153+
place_builder: &PlaceBuilder<'tcx>,
154154
test: &Test<'tcx>,
155155
make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
156156
) {
@@ -727,7 +727,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
727727
let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)`
728728
let consequent_match_pairs = subpatterns.iter().map(|subpattern| {
729729
// e.g., `(x as Variant).0`
730-
let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty);
730+
let place = downcast_place
731+
.clone_project(PlaceElem::Field(subpattern.field, subpattern.pattern.ty));
731732
// e.g., `(x as Variant).0 @ P1`
732733
MatchPair::new(place, &subpattern.pattern, self)
733734
});

compiler/rustc_mir_build/src/build/matches/util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1818
subpatterns
1919
.iter()
2020
.map(|fieldpat| {
21-
let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty);
21+
let place =
22+
place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
2223
MatchPair::new(place, &fieldpat.pattern, self)
2324
})
2425
.collect()
@@ -45,13 +46,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4546
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
4647
let elem =
4748
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
48-
let place = place.clone().project(elem);
49-
MatchPair::new(place, subpattern, self)
49+
MatchPair::new(place.clone_project(elem), subpattern, self)
5050
}));
5151

5252
if let Some(subslice_pat) = opt_slice {
5353
let suffix_len = suffix.len() as u64;
54-
let subslice = place.clone().project(ProjectionElem::Subslice {
54+
let subslice = place.clone_project(PlaceElem::Subslice {
5555
from: prefix.len() as u64,
5656
to: if exact_size { min_length - suffix_len } else { suffix_len },
5757
from_end: !exact_size,
@@ -66,7 +66,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6666
min_length,
6767
from_end: !exact_size,
6868
};
69-
let place = place.clone().project(elem);
69+
let place = place.clone_project(elem);
7070
MatchPair::new(place, subpattern, self)
7171
}));
7272
}

0 commit comments

Comments
 (0)