Skip to content

Commit a2e0cd7

Browse files
committed
Try a cheap bounded walk before the full occur checks
Why: Every variable link in unification runs `occur`, and the Tvar/Tconstr cases also run `deep_occur`. `occur` builds an AVL set of visited nodes along every path and `deep_occur` marks every node and walks the type again to unmark it, although almost all of these walks see a handful of nodes. On a 552 module React app they make 943k and 494k calls, about 0.5 s of 14 s single domain CPU. Both checks now first walk the type with no visited set and no marks, giving up after 256 nodes (which also stops cycles and heavy sharing); when the walk gives up, the unchanged full walk runs. - `occur` in expression mode: a walk over the edges `occur_rec` follows that finishes without meeting the variable proves `occur_rec` would neither raise nor expand anything. Pattern mode, which looks types up, always runs the full walk. - `deep_occur`: the walk only answers "absent". It gives up on meeting the variable and on a node an enclosing walk already marked, so the marks left behind are the same as the full walk's. Review: Can an answer, a diagnostic or an artifact change? No: each fast walk either proves the full walk would do nothing more than it did, or gives up and the full walk runs from scratch. The new fixtures pin the errors, including one past the budget, and the corpus (5,398 artifacts and 1,147 .cmt/.cmti) is byte identical to master. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent d47adb3 commit a2e0cd7

8 files changed

Lines changed: 1026 additions & 3 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Build: skip unneeded work in the type checker's occur checks and when specializing primitives.

‎compiler/ml/ctype.ml‎

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ let is_contractive env p =
13921392

13931393
exception Occur
13941394

1395+
(* [occur_reaches] must follow the same edges in expression mode. *)
13951396
let rec occur_rec env allow_recursive visited ty0 = function
13961397
| {desc = Tlink ty} -> occur_rec env allow_recursive visited ty0 ty
13971398
| ty -> (
@@ -1425,7 +1426,38 @@ let type_changed () = Domain.DLS.get type_changed_key
14251426

14261427
let merge r b = if b then r := true
14271428

1428-
let occur env ty0 ty =
1429+
(* Cheap first passes for [occur] and [deep_occur]: walks with no visited
1430+
set and no marks that give up (raise [Give_up]) after [walk_budget]
1431+
nodes, which also stops cycles and heavy sharing, or when they cannot
1432+
decide; the caller then runs the full walk. A pass that finishes has
1433+
visited every node the full walk would. *)
1434+
exception Give_up
1435+
1436+
let walk_budget = 256
1437+
1438+
let spend budget =
1439+
decr budget;
1440+
if !budget < 0 then raise Give_up
1441+
1442+
(* Must follow the same edges as [occur_rec] in expression mode. Finishing
1443+
proves [occur_rec] would neither meet [ty0] nor find a [Tconstr] cycle,
1444+
its only ways to raise, and it expands types only after raising. *)
1445+
let rec occur_reaches budget ty0 ty =
1446+
match ty.desc with
1447+
| Tlink ty -> occur_reaches budget ty0 ty
1448+
| _ -> (
1449+
if ty == ty0 then raise Occur;
1450+
spend budget;
1451+
match ty.desc with
1452+
| Tobject _ | Tvariant _ -> ()
1453+
| _ -> iter_type_expr (occur_reaches budget ty0) ty)
1454+
1455+
let occur_cannot_fail ty0 ty =
1456+
match occur_reaches (ref walk_budget) ty0 ty with
1457+
| () -> true
1458+
| exception (Occur | Give_up) -> false
1459+
1460+
let occur_full env ty0 ty =
14291461
let allow_recursive = !(umode ()) = Pattern in
14301462
let old = !(type_changed ()) in
14311463
try
@@ -1444,6 +1476,12 @@ let occur env ty0 ty =
14441476
| Occur -> Unify []
14451477
| _ -> exn)
14461478

1479+
(* In pattern mode [occur_rec] looks types up ([is_contractive]), which can
1480+
load interfaces, so only expression mode takes the shortcut. *)
1481+
let occur env ty0 ty =
1482+
if !(umode ()) = Expression && occur_cannot_fail ty0 ty then ()
1483+
else occur_full env ty0 ty
1484+
14471485
let occur_in env ty0 t =
14481486
try
14491487
occur env ty0 t;
@@ -1669,8 +1707,26 @@ let mkvariant fields closed =
16691707

16701708
(**** Unification ****)
16711709

1672-
(* Return whether [t0] occurs in [ty]. Objects are also traversed. *)
1673-
let deep_occur t0 ty =
1710+
(* Must follow the same edges as [deep_occur_marked]. Answers only "absent":
1711+
it gives up on meeting [t0] (where the marked walk stops early) and on a
1712+
node an enclosing walk marked (which the marked walk skips but then
1713+
unmarks in [unmark_type]), so it never leaves marks that differ. *)
1714+
let deep_occur_fast_absent t0 ty =
1715+
let budget = ref walk_budget in
1716+
let rec walk ty =
1717+
let ty = repr ty in
1718+
if ty.level < lowest_level then raise Give_up
1719+
else (
1720+
if ty == t0 then raise Occur;
1721+
spend budget;
1722+
iter_type_expr walk ty)
1723+
in
1724+
match walk ty with
1725+
| () -> true
1726+
| exception (Occur | Give_up) -> false
1727+
1728+
(* [deep_occur_fast_absent] must follow the same edges. *)
1729+
let deep_occur_marked t0 ty =
16741730
let rec occur_rec ty =
16751731
let ty = repr ty in
16761732
if ty.level >= lowest_level then (
@@ -1686,6 +1742,10 @@ let deep_occur t0 ty =
16861742
unmark_type ty;
16871743
true
16881744

1745+
(* Return whether [t0] occurs in [ty]. Objects are also traversed. *)
1746+
let deep_occur t0 ty =
1747+
(not (deep_occur_fast_absent t0 ty)) && deep_occur_marked t0 ty
1748+
16891749
(*
16901750
1. When unifying two non-abbreviated types, one type is made a link
16911751
to the other. When unifying an abbreviated type with a
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/occurs_check_function_type.res:4:22
4+
5+
2 │ contains it. */
6+
3 │ let f = (x: 'a) => {
7+
4 │ let y: 'a => int = x
8+
5 │ y
9+
6 │ }
10+
11+
This value might need to be wrapped in a function that takes an extra
12+
parameter of type 'a
13+
14+
Here's the original error message
15+
This has type: 'a
16+
But it's expected to have type: 'a => int
17+
The type variable 'a occurs inside 'a => int

0 commit comments

Comments
 (0)