Allocate candidate vectors as we sort them
This commit is contained in:
parent
3d3b321c60
commit
8c3688cbb5
7 changed files with 56 additions and 88 deletions
|
@ -1653,10 +1653,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
&'b mut [&'c mut Candidate<'pat, 'tcx>],
|
||||
FxIndexMap<TestBranch<'tcx>, Vec<&'b mut Candidate<'pat, 'tcx>>>,
|
||||
) {
|
||||
// For each of the N possible outcomes, create a (initially empty) vector of candidates.
|
||||
// Those are the candidates that apply if the test has that particular outcome.
|
||||
let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'pat, 'tcx>>> =
|
||||
test.targets().into_iter().map(|branch| (branch, Vec::new())).collect();
|
||||
// For each of the possible outcomes, collect vector of candidates that apply if the test
|
||||
// has that particular outcome.
|
||||
let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'_, '_>>> = Default::default();
|
||||
|
||||
let total_candidate_count = candidates.len();
|
||||
|
||||
|
@ -1668,7 +1667,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
break;
|
||||
};
|
||||
let (candidate, rest) = candidates.split_first_mut().unwrap();
|
||||
target_candidates[&branch].push(candidate);
|
||||
target_candidates.entry(branch).or_insert_with(Vec::new).push(candidate);
|
||||
candidates = rest;
|
||||
}
|
||||
|
||||
|
@ -1809,31 +1808,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
otherwise_block
|
||||
};
|
||||
|
||||
// For each outcome of test, process the candidates that still
|
||||
// apply. Collect a list of blocks where control flow will
|
||||
// branch if one of the `target_candidate` sets is not
|
||||
// exhaustive.
|
||||
// For each outcome of test, process the candidates that still apply.
|
||||
let target_blocks: FxIndexMap<_, _> = target_candidates
|
||||
.into_iter()
|
||||
.map(|(branch, mut candidates)| {
|
||||
if !candidates.is_empty() {
|
||||
let candidate_start = self.cfg.start_new_block();
|
||||
self.match_candidates(
|
||||
span,
|
||||
scrutinee_span,
|
||||
candidate_start,
|
||||
remainder_start,
|
||||
&mut *candidates,
|
||||
);
|
||||
(branch, candidate_start)
|
||||
} else {
|
||||
(branch, remainder_start)
|
||||
}
|
||||
let candidate_start = self.cfg.start_new_block();
|
||||
self.match_candidates(
|
||||
span,
|
||||
scrutinee_span,
|
||||
candidate_start,
|
||||
remainder_start,
|
||||
&mut *candidates,
|
||||
);
|
||||
(branch, candidate_start)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Perform the test, branching to one of N blocks.
|
||||
self.perform_test(span, scrutinee_span, start_block, &match_place, &test, target_blocks);
|
||||
self.perform_test(
|
||||
span,
|
||||
scrutinee_span,
|
||||
start_block,
|
||||
remainder_start,
|
||||
&match_place,
|
||||
&test,
|
||||
target_blocks,
|
||||
);
|
||||
}
|
||||
|
||||
/// Determine the fake borrows that are needed from a set of places that
|
||||
|
|
|
@ -127,6 +127,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
match_start_span: Span,
|
||||
scrutinee_span: Span,
|
||||
block: BasicBlock,
|
||||
otherwise_block: BasicBlock,
|
||||
place_builder: &PlaceBuilder<'tcx>,
|
||||
test: &Test<'tcx>,
|
||||
target_blocks: FxIndexMap<TestBranch<'tcx>, BasicBlock>,
|
||||
|
@ -134,14 +135,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
let place = place_builder.to_place(self);
|
||||
let place_ty = place.ty(&self.local_decls, self.tcx);
|
||||
debug!(?place, ?place_ty);
|
||||
let target_block = |branch| target_blocks[&branch];
|
||||
let target_block = |branch| target_blocks.get(&branch).copied().unwrap_or(otherwise_block);
|
||||
|
||||
let source_info = self.source_info(test.span);
|
||||
match test.kind {
|
||||
TestKind::Switch { adt_def, ref variants } => {
|
||||
// Variants is a BitVec of indexes into adt_def.variants.
|
||||
let num_enum_variants = adt_def.variants().len();
|
||||
debug_assert_eq!(target_blocks.len(), num_enum_variants + 1);
|
||||
let otherwise_block = target_block(TestBranch::Failure);
|
||||
let tcx = self.tcx;
|
||||
let switch_targets = SwitchTargets::new(
|
||||
|
@ -185,7 +185,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
|
||||
TestKind::SwitchInt { ref options } => {
|
||||
// The switch may be inexhaustive so we have a catch-all block
|
||||
debug_assert_eq!(options.len() + 1, target_blocks.len());
|
||||
let otherwise_block = target_block(TestBranch::Failure);
|
||||
let switch_targets = SwitchTargets::new(
|
||||
options
|
||||
|
@ -201,7 +200,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
}
|
||||
|
||||
TestKind::If => {
|
||||
debug_assert_eq!(target_blocks.len(), 2);
|
||||
let success_block = target_block(TestBranch::Success);
|
||||
let fail_block = target_block(TestBranch::Failure);
|
||||
let terminator =
|
||||
|
@ -211,7 +209,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
|
||||
TestKind::Eq { value, ty } => {
|
||||
let tcx = self.tcx;
|
||||
debug_assert_eq!(target_blocks.len(), 2);
|
||||
let success_block = target_block(TestBranch::Success);
|
||||
let fail_block = target_block(TestBranch::Failure);
|
||||
if let ty::Adt(def, _) = ty.kind()
|
||||
|
@ -290,7 +287,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
}
|
||||
|
||||
TestKind::Range(ref range) => {
|
||||
debug_assert_eq!(target_blocks.len(), 2);
|
||||
let success = target_block(TestBranch::Success);
|
||||
let fail = target_block(TestBranch::Failure);
|
||||
// Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
|
||||
|
@ -337,7 +333,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
// expected = <N>
|
||||
let expected = self.push_usize(block, source_info, len);
|
||||
|
||||
debug_assert_eq!(target_blocks.len(), 2);
|
||||
let success_block = target_block(TestBranch::Success);
|
||||
let fail_block = target_block(TestBranch::Failure);
|
||||
// result = actual == expected OR result = actual < expected
|
||||
|
@ -753,33 +748,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Test<'tcx> {
|
||||
pub(super) fn targets(&self) -> Vec<TestBranch<'tcx>> {
|
||||
match self.kind {
|
||||
TestKind::Eq { .. } | TestKind::Range(_) | TestKind::Len { .. } | TestKind::If => {
|
||||
vec![TestBranch::Success, TestBranch::Failure]
|
||||
}
|
||||
TestKind::Switch { adt_def, .. } => {
|
||||
// While the switch that we generate doesn't test for all
|
||||
// variants, we have a target for each variant and the
|
||||
// otherwise case, and we make sure that all of the cases not
|
||||
// specified have the same block.
|
||||
adt_def
|
||||
.variants()
|
||||
.indices()
|
||||
.map(|idx| TestBranch::Variant(idx))
|
||||
.chain([TestBranch::Failure])
|
||||
.collect()
|
||||
}
|
||||
TestKind::SwitchInt { ref options } => options
|
||||
.iter()
|
||||
.map(|(val, bits)| TestBranch::Constant(*val, *bits))
|
||||
.chain([TestBranch::Failure])
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_switch_ty(ty: Ty<'_>) -> bool {
|
||||
ty.is_integral() || ty.is_char()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue