Skip to content

Commit c947090

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 e8ba9f9 commit c947090

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
@@ -1380,6 +1380,7 @@ let is_contractive env p =
13801380

13811381
exception Occur
13821382

1383+
(* [occur_reaches] must follow the same edges in expression mode. *)
13831384
let rec occur_rec env allow_recursive visited ty0 = function
13841385
| {desc = Tlink ty} -> occur_rec env allow_recursive visited ty0 ty
13851386
| ty -> (
@@ -1413,7 +1414,38 @@ let type_changed () = Domain.DLS.get type_changed_key
14131414

14141415
let merge r b = if b then r := true
14151416

1416-
let occur env ty0 ty =
1417+
(* Cheap first passes for [occur] and [deep_occur]: walks with no visited
1418+
set and no marks that give up (raise [Give_up]) after [walk_budget]
1419+
nodes, which also stops cycles and heavy sharing, or when they cannot
1420+
decide; the caller then runs the full walk. A pass that finishes has
1421+
visited every node the full walk would. *)
1422+
exception Give_up
1423+
1424+
let walk_budget = 256
1425+
1426+
let spend budget =
1427+
decr budget;
1428+
if !budget < 0 then raise Give_up
1429+
1430+
(* Must follow the same edges as [occur_rec] in expression mode. Finishing
1431+
proves [occur_rec] would neither meet [ty0] nor find a [Tconstr] cycle,
1432+
its only ways to raise, and it expands types only after raising. *)
1433+
let rec occur_reaches budget ty0 ty =
1434+
match ty.desc with
1435+
| Tlink ty -> occur_reaches budget ty0 ty
1436+
| _ -> (
1437+
if ty == ty0 then raise Occur;
1438+
spend budget;
1439+
match ty.desc with
1440+
| Tobject _ | Tvariant _ -> ()
1441+
| _ -> iter_type_expr (occur_reaches budget ty0) ty)
1442+
1443+
let occur_cannot_fail ty0 ty =
1444+
match occur_reaches (ref walk_budget) ty0 ty with
1445+
| () -> true
1446+
| exception (Occur | Give_up) -> false
1447+
1448+
let occur_full env ty0 ty =
14171449
let allow_recursive = !(umode ()) = Pattern in
14181450
let old = !(type_changed ()) in
14191451
try
@@ -1432,6 +1464,12 @@ let occur env ty0 ty =
14321464
| Occur -> Unify []
14331465
| _ -> exn)
14341466

1467+
(* In pattern mode [occur_rec] looks types up ([is_contractive]), which can
1468+
load interfaces, so only expression mode takes the shortcut. *)
1469+
let occur env ty0 ty =
1470+
if !(umode ()) = Expression && occur_cannot_fail ty0 ty then ()
1471+
else occur_full env ty0 ty
1472+
14351473
let occur_in env ty0 t =
14361474
try
14371475
occur env ty0 t;
@@ -1657,8 +1695,26 @@ let mkvariant fields closed =
16571695

16581696
(**** Unification ****)
16591697

1660-
(* Return whether [t0] occurs in [ty]. Objects are also traversed. *)
1661-
let deep_occur t0 ty =
1698+
(* Must follow the same edges as [deep_occur_marked]. Answers only "absent":
1699+
it gives up on meeting [t0] (where the marked walk stops early) and on a
1700+
node an enclosing walk marked (which the marked walk skips but then
1701+
unmarks in [unmark_type]), so it never leaves marks that differ. *)
1702+
let deep_occur_fast_absent t0 ty =
1703+
let budget = ref walk_budget in
1704+
let rec walk ty =
1705+
let ty = repr ty in
1706+
if ty.level < lowest_level then raise Give_up
1707+
else (
1708+
if ty == t0 then raise Occur;
1709+
spend budget;
1710+
iter_type_expr walk ty)
1711+
in
1712+
match walk ty with
1713+
| () -> true
1714+
| exception (Occur | Give_up) -> false
1715+
1716+
(* [deep_occur_fast_absent] must follow the same edges. *)
1717+
let deep_occur_marked t0 ty =
16621718
let rec occur_rec ty =
16631719
let ty = repr ty in
16641720
if ty.level >= lowest_level then (
@@ -1674,6 +1730,10 @@ let deep_occur t0 ty =
16741730
unmark_type ty;
16751731
true
16761732

1733+
(* Return whether [t0] occurs in [ty]. Objects are also traversed. *)
1734+
let deep_occur t0 ty =
1735+
(not (deep_occur_fast_absent t0 ty)) && deep_occur_marked t0 ty
1736+
16771737
(*
16781738
1. When unifying two non-abbreviated types, one type is made a link
16791739
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)