Skip to content

Commit f93cbf6

Browse files
committed
Short-circuit DefIdForest::intersection()
If the forest is already empty, there is no point in intersecting further. Also handle the first element separately, so we don't compute an unnecessary intersection between the full forest and the first element, which is always equal to the first element.
1 parent 9689ada commit f93cbf6

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/librustc/ty/inhabitedness/def_id_forest.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,21 @@ impl<'a, 'gcx, 'tcx> DefIdForest {
7474
iter: I) -> DefIdForest
7575
where I: IntoIterator<Item=DefIdForest>
7676
{
77-
let mut ret = DefIdForest::full(tcx);
77+
let mut iter = iter.into_iter();
78+
let mut ret = if let Some(first) = iter.next() {
79+
first
80+
} else {
81+
return DefIdForest::full(tcx);
82+
};
83+
7884
let mut next_ret = SmallVec::new();
7985
let mut old_ret: SmallVec<[DefId; 1]> = SmallVec::new();
8086
for next_forest in iter {
87+
// No need to continue if the intersection is already empty.
88+
if ret.is_empty() {
89+
break;
90+
}
91+
8192
for id in ret.root_ids.drain() {
8293
if next_forest.contains(tcx, id) {
8394
next_ret.push(id);

0 commit comments

Comments
 (0)