8000 Optimize `ZIO#provideSomeEnvironment` and `FiberRef#locallyWith` by kyri-petrou · Pull Request #9652 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Optimize ZIO#provideSomeEnvironment and FiberRef#locallyWith #9652

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

Conversation

kyri-petrou
Copy link
Contributor

ZIO#provideSomeEnvironment is one of the methods that can be commonly evaluated (depending on the workflow) since it's used by ZIO.scoped. Currently it requires an invocation of FiberRef#get and another of FiberRef#locally. We can "optimize" it by using FiberRef#locallyWith.

However, since locallyWith is actually implemented as getWith + locally there isn't much benefit of simply changing it. This PR also optimizes locallyWith by making it "unfinal" and overriding its implementation to avoid the additional FiberRef#getWith.

I also made some other minor optimizations such as making the methods in the anonymous class final (since for some reason the Scala compiler doesn't make anonymous classes final 🙄)

@@ -467,54 +467,73 @@ object FiberRef {
self =>
type Patch = Patch0

def combine(first: Patch, second: Patch): Patch =
final def combine(first: Patch, second: Patch): Patch =
Copy link
Member
@guizmaii guizmaii Feb 27, 2025

Choose a reason for hiding this comment

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

What about using a final class here instead of an anonymous one? 🤔
Anonymous classes generates quite ugly stack traces

final class PatchFiber[Value0, Patch0] extends FiberRef[Value0] {
  type Patch = Patch0
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that too, I'll change it 👍

hearnadam
hearnadam previously approved these changes Feb 28, 2025
ZIO.uninterruptibleMask { restore =>
restore(zio).exitWith { exit =>
val oldValue = oldRefs.getOrNull(self)
if (oldValue == null) fiberState.resetFiberRef(self)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (oldValue == null) fiberState.resetFiberRef(self)
if (oldValue eq null) fiberState.resetFiberRef(self)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oldValue is not an AnyRef so we can't use eq

Comment on lines +1290 to +1291
FiberRef.currentEnvironment
.locallyWith(f.asInstanceOf[ZEnvironment[Any] => ZEnvironment[Any]])(self.asInstanceOf[ZIO[Any, E, A]])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the casting necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because FiberRef.currentEnvironment contains a ZEnvironment[Any] (which is not very correct I know but it's a package-private val) so we have to cast the function to ZEvironment[Any] => ZEvironment[Any] for the code to compile

Copy link
Member
@guizmaii guizmaii left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@kyri-petrou kyri-petrou merged commit 88fab58 into zio:series/2.x Feb 28, 2025
18 checks passed
@kyri-petrou kyri-petrou deleted the optimize-locally-with-and-provide-some-env branch February 28, 2025 08:13
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