Skip to content

Make Hashtbl.find_all tail recursive via a tail_mod_cons annotation#11354

Merged
xavierleroy merged 4 commits intoocaml:trunkfrom
ferminr:stdlib-hashtbl-findall-tailrec
Jul 22, 2022
Merged

Make Hashtbl.find_all tail recursive via a tail_mod_cons annotation#11354
xavierleroy merged 4 commits intoocaml:trunkfrom
ferminr:stdlib-hashtbl-findall-tailrec

Conversation

@ferminr
Copy link
Copy Markdown
Contributor

@ferminr ferminr commented Jun 23, 2022

This fixes #8676, which had to wait for 5.x.

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 23, 2022

Out of curiosity, did you test that it is indeed tail-recursive? (How?)

@ferminr
Copy link
Copy Markdown
Contributor Author

ferminr commented Jun 23, 2022

Out of curiosity, did you test that it is indeed tail-recursive? (How?)

Yes, I tested it. The following example terminates with the new code, but results in a stack overflow with the 4.14 Stdlib (tested with ocamlc and ocamlopt):

let h = Hashtbl.create 2 in
  for i = 0 to 1_000_000 do Hashtbl.add h 0 i done;
  Hashtbl.find_all h 0

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 24, 2022

On my machine, this example terminates correctly on trunk without any additional change, with both ocamlc and ocamlopt.

@lthls
Copy link
Copy Markdown
Contributor

lthls commented Jun 24, 2022

On my machine, this example terminates correctly on trunk without any additional change, with both ocamlc and ocamlopt.

Isn't that because stacks are heap-allocated on trunk, so we never get stack overflows ?

@hhugo
Copy link
Copy Markdown
Contributor

hhugo commented Jun 24, 2022

Out of curiosity, did you test that it is indeed tail-recursive? (How?)

Aren't there warnings 71 and 72 for this ?

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 24, 2022

Aren't there warnings 71 and 72 for this ?

The warnings do not protect, for example, against a typo in the attribute name.

@xavierleroy
Copy link
Copy Markdown
Contributor

In OCaml 5 stack limits are quite large, so it's better to explicitly set a low stack limit to run these kind of tests, e.g.

OCAMLRUNPARAM="l=100000" ./mytest.exe

@ferminr
Copy link
Copy Markdown
Contributor Author

ferminr commented Jun 25, 2022

In OCaml 5 stack limits are quite large, so it's better to explicitly set a low stack limit to run these kind of tests, e.g.

OCAMLRUNPARAM="l=100000" ./mytest.exe

Aha! That makes testing easier. On my machine, using trunk and setting 10000 for both the stack limit and the number of iterations in the test code, I get a a stack overflow. Using trunk with the change in Stdlib, I can keep the low stack limit and increase the number of iterations by 100x and the test runs fine.

Copy link
Copy Markdown
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

I am now convinced thanks to the testing report. Thanks!

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 25, 2022

@ferminr would you like to include a Changes entry for OCaml 5.1, or is it intentional not to?

@ferminr
Copy link
Copy Markdown
Contributor Author

ferminr commented Jun 25, 2022

@ferminr would you like to include a Changes entry for OCaml 5.1, or is it intentional not to?

Ah, yes, I'll add an entry in Changes. What do folks normally do? Add the entry when the PR is first submitted, or wait until it's reviewed and approved?

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Jun 25, 2022

Does the approval of this PR mean we are now allowed to apply TRMC to List.map and friends?

@gasche
Copy link
Copy Markdown
Member

gasche commented Jun 26, 2022

@nojb I don't see why not, but I find it reassuring that it would be for 5.1 rather than 5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-tail recursive Hashtable find_all function

6 participants