Consider rewriting recursive ZIO methods to iterate instead
I'm opening this as an issue so that we can track it (and so that I don't forget about it 😅)
While working on #9081 I realised that we have a fair amount of ZIO methods that use recursion when it can be avoided. e.g., the current implementation of forever:
final def forever(implicit trace: Trace): ZIO[R, E, Nothing] = {
lazy val loop: ZIO[R, E, Nothing] = self *> ZIO.yieldNow *> loop
loop
}
Can be rewritten to:
final def forever(implicit trace: Trace): ZIO[R, E, Nothing] =
ZIO.whileLoop(true)(self)(_ => ()).asInstanceOf[ZIO[R, E, Nothing]]
This is much more performant as it doesn't require yielding in order to avoid iterating deeper into the FiberRuntime stack.
Interestingly, we already do adopt this approach in some methods (e.g., iterate) so it might be good to adopt it wherever possible
This may require iterate be changed to yield every n iterations.
/bounty $450
💎 $450 bounty • ZIO
Steps to solve:
- Start working: Comment
/attempt #9082with your implementation plan - Submit work: Create a pull request including
/claim #9082in the PR body to claim the bounty - Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts
Thank you for contributing to zio/zio!
Add a bounty • Share on socials
| Attempt | Started (GMT+0) | Solution |
|---|---|---|
| 🔴 @yanisoln | Sep 4, 2024, 12:48:34 AM | WIP |
| 🟢 @tmccombs | #9131 |
/attempt #9082
| Algora profile | Completed bounties | Tech | Active attempts | Options |
|---|---|---|---|---|
| @yanisoln | 1 bounty from 1 project | Cancel attempt |
@yanisoln fyi: There is already an open PR
💡 @tmccombs submitted a pull request that claims the bounty. You can visit your bounty board to reward.
@tmccombs: You've been awarded a $450 bounty by ZIO! 👉 Complete your Algora onboarding to collect the bounty.