stdlib
stdlib copied to clipboard
tco: add larger tests where recursion happens
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.
Great idea!
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).
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.
Good suggestion @NicklasXYZ , thank you
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.
@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
}
}
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.
#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
Thanks gang
#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.