-
Notifications
You must be signed in to change notification settings - Fork 16
Make recursive calls to flatMap stack safe #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -311,4 +311,64 @@ describe("Future Belt.Result", () => { | |
| |> finish | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| testAsync("value recursion is stack safe", finish => { | ||
| let next = x => Future.value(~executor=`trampoline, x + 1); | ||
| let numberOfLoops = 10000; | ||
| let rec loop = x => { | ||
| next(x) | ||
| ->Future.flatMap(x => Future.value(x + 1)) | ||
| ->Future.map(x => x + 1) | ||
| ->Future.flatMap(x => | ||
| Future.value(x + 1)->Future.flatMap(x => Future.value(x + 1)) | ||
| ) | ||
| ->Future.flatMap(x' => | ||
| if (x' > numberOfLoops) { | ||
| Future.value(x'); | ||
| } else { | ||
| loop(x'); | ||
| } | ||
| ); | ||
| }; | ||
| loop(0) | ||
| ->Future.get(r => | ||
| r |> expect |> toBeGreaterThan(numberOfLoops) |> finish | ||
| ); | ||
| }); | ||
|
|
||
| testAsync("async recursion is stack safe", finish => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make a test using the default executor to ensure it blows the stack and we're not just always using the trampoline
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See "value recursion blows the stack with default executor" |
||
| let next = x => delay(~executor=`trampoline, 1, () => x + 1); | ||
| let numberOfLoops = 1000; | ||
| let rec loop = x => { | ||
| next(x) | ||
| ->Future.flatMap(x' => | ||
| if (x' == numberOfLoops) { | ||
| Future.value(x'); | ||
| } else { | ||
| loop(x'); | ||
| } | ||
| ); | ||
| }; | ||
| loop(0) | ||
| ->Future.get(r => r |> expect |> toEqual(numberOfLoops) |> finish); | ||
| }); | ||
|
|
||
| test("value recursion blows the stack with default executor", () => { | ||
| let next = x => Future.value(x + 1); | ||
| let numberOfLoops = 10000; | ||
| let rec loop = x => { | ||
| next(x) | ||
| ->Future.flatMap(x' => | ||
| if (x' > numberOfLoops) { | ||
| Future.value(x'); | ||
| } else { | ||
| loop(x'); | ||
| } | ||
| ); | ||
| }; | ||
| expect(() => | ||
| loop(0) | ||
| ) | ||
| |> toThrowMessage("Maximum call stack size exceeded"); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a deeply nested chain and test that we don't blow the stack there. Something like the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done