Skip to content

Commit 0471b32

Browse files
committed
Reorganize match construction to be O(n) instead of O(n^2). Whereas
before we iterated over the test and each outcome thereof, and then checked processed every candidate against this outcome, we now organize the walk differently. Instead, we visit each candidate and say "Here is the test being performed. Figure out the resulting candidates for each possible outcome and add yourself into the appropriate places."
1 parent 61884e5 commit 0471b32

File tree

2 files changed

+160
-233
lines changed

2 files changed

+160
-233
lines changed

src/librustc_mir/build/matches/mod.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,20 @@ impl<'a,'tcx> Builder<'a,'tcx> {
345345
debug!("match_candidates: test={:?} match_pair={:?}", test, match_pair);
346346
let target_blocks = self.perform_test(block, &match_pair.lvalue, &test);
347347

348-
for (outcome, target_block) in target_blocks.into_iter().enumerate() {
349-
let applicable_candidates: Vec<_> =
350-
candidates.iter()
351-
.filter_map(|candidate| {
352-
self.candidate_under_assumption(&match_pair.lvalue,
353-
&test.kind,
354-
outcome,
355-
candidate)
356-
})
357-
.collect();
358-
self.match_candidates(span, arm_blocks, applicable_candidates, target_block);
348+
let mut target_candidates: Vec<_> = (0..target_blocks.len()).map(|_| vec![]).collect();
349+
350+
for candidate in &candidates {
351+
self.sort_candidate(&match_pair.lvalue,
352+
&test,
353+
candidate,
354+
&mut target_candidates);
355+
}
356+
357+
for (target_block, target_candidates) in
358+
target_blocks.into_iter()
359+
.zip(target_candidates.into_iter())
360+
{
361+
self.match_candidates(span, arm_blocks, target_candidates, target_block);
359362
}
360363
}
361364

0 commit comments

Comments
 (0)