stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

tco: add larger tests where recursion happens

Open inoas opened this issue 3 years ago • 5 comments
trafficstars

is this a good idea?:

add larger tests to recursive functions:

  • makes sure tco happens now
  • makes sure tco happens in future
  • helps detecting if runtime implementation has tco issues

Note: Once I have time I’ll get at this unless anyone beats me to it.

inoas avatar Jul 26 '22 06:07 inoas

Great idea!

lpil avatar Jul 27 '22 17:07 lpil

Just a suggestion, but maybe it could be possible to attach a comment to the test that tco is tested? To me it is otherwise not apparent that is actually what is being tested. E.g. here https://github.com/gleam-lang/stdlib/blob/main/test/gleam/list_test.gleam#L312 (and 16999 looks like an oddly specific number to test tco).

NicklasXYZ avatar Aug 20 '22 12:08 NicklasXYZ

16999 is a number I think one higher or slightly higher than what would make the iteration crash on Firefox or Safari, think it was Firefox AFAIR. It was higher than the number where NodeJS/Chrome crashed, so I picked it in case we ever run tests against Firefox's JS engine (or rather against the engine which takes the most iterations till crash on non TCOed recursion).

I ll keep your notice in mind once/if I get to this issue here.

inoas avatar Aug 21 '22 10:08 inoas

Good suggestion @NicklasXYZ , thank you

lpil avatar Aug 22 '22 17:08 lpil

I have started with this here: https://github.com/gleam-lang/stdlib/pull/340

I have been using watchexec --no-shell --postpone --watch-when-idle -- sh -c 'gleam test --target javascript; gleam test --target erlang' which helped speed things up a lot, thanks to @tynanbe for a quick guide on watchexec.

inoas avatar Sep 15 '22 20:09 inoas

@NicklasXYZ

I have redone some testing on the barriers where the stack size crashes on target JavaScript:

import gleam/io

pub fn main() {
  io.println("Attempting 10_000 iterations...")
  // Crashes in Chrome 106.0.5249.119
  // Crashes on Node v16.15.0, v18.11.0 and v19.0.0
  some_recursion(10_000)

  io.println("Attempting 35_000 iterations...")
  // Crashes in Firefox 106.0.1
  some_recursion(35_000)

  // Crashes in Safari 16.0
  io.println("Attempting 40_000 iterations...")
  some_recursion(40000)

  io.println("Done!")
}

fn some_recursion(calls_todo) {
  case calls_todo == 0 {
    True -> 0
    False -> some_recursion(calls_todo - 1) + 1
  }
}

Test here: https://johndoneth.github.io/gleam-playground/?s=JYWwDg9gTgLgBAcwDYFMCGID0wIChdgCuARnAGYB2cIawFAFAJRwDeuccOAdGFHTEgYAiAIIwYKcDDoI4ARgAMAfQWrOEqGmkQKAZy4GhjdnEyY4AYU26AFil2cqFm1AggU8hQDYuCrgFYAJgAWAE4uOTlQkzNLazsHHTgAOQgAEw8ANzkfOX9fABo4bIAOCLlfODQKNOKo318TXTcUJSgUAGNCKF0cBkUVVWMTbl5%2BQXpRcUkwaQpZAGZ%2FQYV1FE1tPQMuIxjzKzRbe0c4ADFgdrIIAA9PHz85Jpa2zu7enXolleGOWIOjhx0OAAZTQZDQfHk9xGEB4fAoAmEYgkUhkcGCylUq2AGi0fX0hmMHGa7heXR6fXoGKxP04sLGCImQgAIjoUABCXYAX3wlDgJNa7XJ7wYHTQSCQuiUMHSEGYbA4Yt0HjFEqlMrSEDgAF5tXBVgqOHAACpQQgeAC0AD59SYOKdxcq4Nb%2Bc8hW9KarJdLZc75MwANTyEw8rlAA%3D%3D%3D

inoas avatar Oct 23 '22 14:10 inoas

https://github.com/gleam-lang/stdlib/pull/340 got merged but I still want to add some more tests for at least string, string_builder, interator and maybe map modules, I think.

inoas avatar Oct 27 '22 22:10 inoas

#340 got merged but I still want to add some more tests for at least string, string_builder, interator and maybe map modules, I think.

@inoas have you written any new TCO tests in these modules? Because, I have some free time and I could start working on adding the tests to one of these modules

giacomocavalieri avatar Apr 25 '23 18:04 giacomocavalieri

Thanks gang

lpil avatar Apr 25 '23 20:04 lpil

#340 got merged but I still want to add some more tests for at least string, string_builder, interator and maybe map modules, I think.

@inoas have you written any new TCO tests in these modules? Because, I have some free time and I could start working on adding the tests to one of these modules

No I haven't. And I do not have any spare time in foreseeable future.

inoas avatar Apr 26 '23 07:04 inoas