Skip to content

Commit

Permalink
perf: Avoid recursing more in occurs check
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jan 27, 2019
1 parent e5f086c commit c0acb06
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
25 changes: 21 additions & 4 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,6 @@ where
I: 'a,
{
match **typ {
Type::Forall(_, ref typ) => f.walk(typ),
Type::Function(_, ref arg, ref ret) => {
f.walk(arg);
f.walk(ret);
Expand All @@ -2538,7 +2537,10 @@ where
f.walk(a);
}
}
Type::Record(ref row) | Type::Variant(ref row) | Type::Effect(ref row) => f.walk(row),
Type::Forall(_, ref typ)
| Type::Record(ref typ)
| Type::Variant(ref typ)
| Type::Effect(ref typ) => f.walk(typ),
Type::ExtendRow {
ref types,
ref fields,
Expand Down Expand Up @@ -3442,9 +3444,24 @@ where
}
}

impl<'a, I, T, F: ?Sized> Walker<'a, T> for F
pub struct FlagsVisitor<F: ?Sized>(pub Flags, pub F);

impl<'a, T, F> Walker<'a, T> for FlagsVisitor<F>
where
F: ?Sized + FnMut(&'a T),
T: TypeExt + 'a,
{
fn walk(&mut self, typ: &'a T) {
if self.0.intersects(typ.flags()) {
(self.1)(typ);
walk_type_(typ, self);
}
}
}

impl<'a, I, T, F> Walker<'a, T> for F
where
F: FnMut(&'a T),
F: ?Sized + FnMut(&'a T),
T: Deref<Target = Type<I, T>> + 'a,
I: 'a,
{
Expand Down
20 changes: 8 additions & 12 deletions check/src/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where
T: Substitutable,
{
fn walk(&mut self, typ: &'t T) {
if self.occurs {
if !typ.contains_variables() || self.occurs {
return;
}
let typ = self.subs.real(typ);
Expand All @@ -174,17 +174,13 @@ where
}
}

if typ.contains_variables() {
let mut occurs = Occurs {
occurs: false,
var: var,
subs: subs,
};
occurs.walk(typ);
occurs.occurs
} else {
false
}
let mut occurs = Occurs {
occurs: false,
var: var,
subs: subs,
};
occurs.walk(typ);
occurs.occurs
}

/// Specialized union implementation which makes sure that variables with a higher level always
Expand Down

0 comments on commit c0acb06

Please sign in to comment.