zio icon indicating copy to clipboard operation
zio copied to clipboard

Consider rewriting recursive ZIO methods to iterate instead

Open kyri-petrou opened this issue 1 year ago • 5 comments

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

kyri-petrou avatar Aug 06 '24 17:08 kyri-petrou

This may require iterate be changed to yield every n iterations.

/bounty $450

jdegoes avatar Sep 03 '24 16:09 jdegoes

💎 $450 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #9082 with your implementation plan
  2. Submit work: Create a pull request including /claim #9082 in the PR body to claim the bounty
  3. 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 bountyShare on socials

Attempt Started (GMT+0) Solution
🔴 @yanisoln Sep 4, 2024, 12:48:34 AM WIP
🟢 @tmccombs #9131

algora-pbc[bot] avatar Sep 03 '24 16:09 algora-pbc[bot]

/attempt #9082

Algora profile Completed bounties Tech Active attempts Options
@yanisoln 1 bounty from 1 project
Cancel attempt

yanisoln avatar Sep 04 '24 00:09 yanisoln

@yanisoln fyi: There is already an open PR

987Nabil avatar Sep 04 '24 07:09 987Nabil

💡 @tmccombs submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] avatar Sep 08 '24 04:09 algora-pbc[bot]

@tmccombs: You've been awarded a $450 bounty by ZIO! 👉 Complete your Algora onboarding to collect the bounty.

algora-pbc[bot] avatar Nov 24 '24 01:11 algora-pbc[bot]

🎉🎈 @tmccombs has been awarded $450! 🎈🎊

algora-pbc[bot] avatar Nov 25 '24 07:11 algora-pbc[bot]