Skip to content

occur_rec on a graph rather than a tree - #13866

Merged
Octachron merged 3 commits into
ocaml:trunkfrom
samsa1:occur_rec_on_graph
Apr 16, 2025
Merged

Octachron merged 3 commits into
ocaml:trunkfrom
samsa1:occur_rec_on_graph

Conversation

@samsa1

@samsa1 samsa1 commented Mar 13, 2025

Copy link
Copy Markdown
Contributor

When unifying two types we check that this does not create a recursive type (if -rectypes is not enabled).

However this test can be quite costly as reported by @diremy.

For example if we take the following example :

let unif x y = (fun k z -> k (z x) (z y)) (fun x y -> y) (fun z -> z)

let mairson() =
  let _ =
    let d = unif in
    let d x = d (d x) in (* repeated line *)
    d (fun z -> z) in
  () (* e *)

If we repeat line 6 we get a catastrophic complexity :

  • 3 times : 3.7ms in typing
  • 4 times : 0.6s in typing
  • 5 times : did not finish after more than 10 minutes, I expect the actual time to be around 20minutes
    On the other hand, with -rectypes, repeating this line 14 times leads to a whole compilation time below 0.1s.

A first fix for this problem is to do the occurrence check on a graph rather than on a tree. With this optimization we get the following performances on the test presented above :

  • 3 times : 1.2ms in typing
  • 4 times : 1.6ms in typing
  • 10 times : 1.3s
  • 13 times : 120s

As a side note : I checked and this does not seam to impact the time needed to compile the testsuite

Comment thread typing/ctype.ml Outdated
| Tobject _ | Tvariant _ ->
()
| _ ->
if allow_recursive || TypeSet.mem ty visited then () else begin

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where to leave this comment so I'll put it here :

I'm surprised by this line. Here we say that if we see a recursive type when checking that we don't create one we can just ignore it. I know that rejecting it at that moment without having a good location for an error message might be problematic however isn't it better to reject it at that moment than to accept the code ?

The only tests in the testsuite affected by adding an assert allow_recursive here are tests identified as bugs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is typing-polyvariants-bug-2/pr3918.ml is not exactly a bug but it is probably fine to reject a recursive type which is not allowed due to a missing cmi file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garrigue , can you think of any other circumstances when we may have a cycle not guarded by an object or variant types?
Otherwise, I would be tempted to consider that it is better to fail on those kind of unguarded cycles created by missing cmis.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote some documentation on what I knew of recursive type situations in OCaml in typedecl.ml (to document check_well_founded):

(* Note: Well-foundedness for OCaml types

The documentation agrees with your intuition that in absence of -rectypes, recursive types are only allowed to go through polymorphic variants and object types. (To check this, look for the occurrences of " guarded" in the documentation, which list the only sources of allowed cycles; when -rectypes is used, the "rectypes-guarded" constructs are also allowed.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exploring the history

Thus it seems that we could remove the support for pre-existing cycles in occur_rec, but in a later PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For whether it is ok to fail when discovering a pre-existing cycle, this requires checking a number of things: namely, interactions with the counter-example mode (which is allowed to create such cycles), and mixing of files compiled with an without -rectypes. I don't have all the details in mind, but this would depend on this.
Also, logically, it should probably be an assertion rather than an occur check error, since it would not be supposed to happen.

Comment thread typing/ctype.ml Outdated
Tconstr(p, _tl, _abbrev) ->
if allow_recursive && is_contractive env p then () else
begin try
if TypeSet.mem ty visited then raise Occur;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use two distinct marks (a visited mark and a seen mark) to remove the TypeSet entirely.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I visited is handled as a mark we would need to remove the mark whenever an exception is raised (which seems quite costly).

However I fully agree that mark is not a good name and seen is much better

@garrigue

Copy link
Copy Markdown
Contributor

The logic seems fine to me.
As for why it was not done before, one first aspect is that the code used to be much more complicated, with only Jérome knowing why.
Also, as you can see, due to the possibility to expand in case of failure, one cannot mark before recursing, as done usually. Marking afterwards seems fine, but this means using two distinct mechanisms for pre-existing recursion and sharing. I understand know that the extra complexity is worthwhile if there is a lot of sharing.

@Octachron

Copy link
Copy Markdown
Member

I am currently planning to run a compiler benchmark on the change this weekend as a sanity check before merging on Monday.

@Octachron

Copy link
Copy Markdown
Member

Looking at performance number while compiling a hundred of opam packages, there is an expected increase in the time spent in occur_rec, which is not visible in total type checking time

  • average increase of occur time: +%13
  • average increase of typechecking time: +0.092%
  • average increase of total compilation time: -2%

Thus the changes look ready to merge (once the Changes conflict is fixed).

@samsa1
samsa1 force-pushed the occur_rec_on_graph branch from 4637f69 to db20341 Compare April 14, 2025 13:04
@Octachron
Octachron merged commit ec3373c into ocaml:trunk Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants