Skip to content

Commit 9e395df

Browse files
committed
Anticipate allocation sizes
1 parent 1bcbb7c commit 9e395df

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
599599
// an or-pattern. Panics if `self` is empty.
600600
fn expand_or_pat<'a>(&'a self) -> impl Iterator<Item = PatStack<'p, 'tcx>> + Captures<'a> {
601601
self.head().flatten_or_pat().into_iter().map(move |pat| {
602-
let mut new_pats = smallvec![pat];
603-
new_pats.extend_from_slice(&self.pats[1..]);
604-
PatStack { pats: new_pats }
602+
let mut new = self.clone();
603+
new.pats[0] = pat;
604+
new
605605
})
606606
}
607607

@@ -732,18 +732,11 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
732732
}
733733

734734
/// Build a new matrix from an iterator of `MatchArm`s.
735-
fn new<'a>(
736-
cx: &MatchCheckCtxt<'p, 'tcx>,
737-
iter: impl Iterator<Item = &'a MatchArm<'p, 'tcx>>,
738-
scrut_ty: Ty<'tcx>,
739-
) -> Self
740-
where
741-
'p: 'a,
742-
{
735+
fn new(cx: &MatchCheckCtxt<'p, 'tcx>, arms: &[MatchArm<'p, 'tcx>], scrut_ty: Ty<'tcx>) -> Self {
743736
let wild_pattern = cx.pattern_arena.alloc(DeconstructedPat::wildcard(scrut_ty, DUMMY_SP));
744737
let wildcard_row = PatStack::from_pattern(wild_pattern);
745-
let mut matrix = Matrix { rows: vec![], wildcard_row };
746-
for (row_id, arm) in iter.enumerate() {
738+
let mut matrix = Matrix { rows: Vec::with_capacity(arms.len()), wildcard_row };
739+
for (row_id, arm) in arms.iter().enumerate() {
747740
let v = MatrixRow {
748741
pats: PatStack::from_pattern(arm.pat),
749742
parent_row: row_id, // dummy, we won't read it
@@ -806,7 +799,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
806799
ctor: &Constructor<'tcx>,
807800
) -> Matrix<'p, 'tcx> {
808801
let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor);
809-
let mut matrix = Matrix { rows: vec![], wildcard_row };
802+
let mut matrix = Matrix { rows: Vec::new(), wildcard_row };
810803
for (i, row) in self.rows().enumerate() {
811804
if ctor.is_covered_by(pcx, row.head().ctor()) {
812805
let new_row = row.pop_head_constructor(pcx, ctor, i);
@@ -1386,7 +1379,7 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
13861379
arms: &[MatchArm<'p, 'tcx>],
13871380
scrut_ty: Ty<'tcx>,
13881381
) -> UsefulnessReport<'p, 'tcx> {
1389-
let mut matrix = Matrix::new(cx, arms.iter(), scrut_ty);
1382+
let mut matrix = Matrix::new(cx, arms, scrut_ty);
13901383
let non_exhaustiveness_witnesses =
13911384
compute_exhaustiveness_and_reachability(cx, &mut matrix, true);
13921385

0 commit comments

Comments
 (0)