8000 Use `Exit.succeed` in more places by kyri-petrou · Pull Request #9418 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use Exit.succeed in more places #9418

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

Merged
merged 2 commits into from
Dec 17, 2024
Merged

Conversation

kyri-petrou
Copy link
Contributor

AFAICT all these places are safe to use Exit.succeed as the values / functions are already suspended in another effect

@kyri-petrou kyri-petrou requested a review from guizmaii December 16, 2024 14:08
@@ -970,7 +970,7 @@ sealed trait ZIO[-R, +E, +A]
* Returns an effect whose success is mapped by the specified `f` function.
*/
def map[B](f: A => B)(implicit trace: Trace): ZIO[R, E, B] =
flatMap(a => ZIO.succeed(f(a)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will greatly reduce the instances of ZIO.Sync... should be interesting to see if this impacts runtime performance. Does FiberRuntime always prioritize ZIO.flatMap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, the FiberRuntime runloop checks first whether the effect is ZIO.Sync and then Exit. However, after all these changes to use Exit a lot more, I'm contemplating to move Exit to be the first one to be checked. WDUT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to say. I figure 90%+ of user code is probably not explicitly using Exit, but I could be wrong. I do think we should generally optimized for deeply nested ZIOs (via map/flatMap) as usercode is often not optimized to reduce that.

It would be cool if we could get stats on suspension depth and ask users to collect this information...

Comment on lines -6532 to -6534
override final def map[B](f: A => B)(implicit trace: Trace): ZIO[Any, E, B] =
mapExit(f)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hearnadam since you already reviewed just wanted to point out that I made this change (also with fold) since your last review. I think this overriding is no longer required because we already override flatMap to produce an exit. This allows us to make the map and fold methods final on ZIO which means the JVM can apply better optimizations / inlining as the method is no longer overwritten.

It does result in slightly more allocations in cases that map / fold is used on an Exit, but to be honest I think those cases not very common and we can always use mapExit / foldExit on them.

@kyri-petrou kyri-petrou merged commit 814e5e5 into zio:series/2.x Dec 17, 2024
18 checks passed
@kyri-petrou kyri-petrou deleted the use-more-exits branch December 17, 2024 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0