-
Notifications
You must be signed in to change notification settings - Fork 522
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
optimize IO.whenA
#4135
optimize IO.whenA
#4135
Conversation
Interesting, that must be because of the See also: |
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.
Shall we add a test case based on your example?
We could do that, but this slimmed down version still takes 20 seconds to complete on my machine. And that's in the happy path where it doesn't fail or use a ridiculous amount of memory. def loop(i: Long): IO[Unit] =
IO.unit >> IO.whenA(i < 1_100_000_000)(loop(i + 1)) |
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.
Ah yes, d'oh, good point. Probably not worth the strain on CI.
Should We do something about def void: IO[Unit] =
5 - map(_ => ())
6 + isInstanceOf[IO[Unit]] match {
7 + case true => this.asInstanceOf[IO[Unit]]
8 + case _ => map(_ => ())
9 + } |
@lenguyenthanh Unfortunately this won't work because type parameters are erased at runtime, so it will always be |
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.
This makes quite a bit of sense honestly. Also it raises the question of when and why whenA
would actually be desirable…
Can you add an override in the Async
typeclass implementation for both methods as well? That will allow the optimization to work in polymorphic contexts.
The problem is like @armanbilge pointed out that the typeclass method takes a |
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.
Ooooooh yeah this is exceptionally annoying. Both because the signature in Cats is definitely wrong, but also because we're inconsistent with it and I didn't realize. At any rate, bigger fish than we can fry right now.
This might be a matter of opinion, see a discussion on this topic in typelevel/cats#4352 (comment). |
I noticed that this simple recursive loop
ends with
While the equivalent
if else
code seems to keep running without noticeable GC pauses.