Skip to content

Commit

Permalink
Avoid allocation in ZEnvironment.get (#8713)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr committed Apr 9, 2024
1 parent 1c7363f commit 93be9ea
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/zio/ZEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ final class ZEnvironment[+R] private (
getOrElse(tag, throw new Error(s"Defect in zio.ZEnvironment: Could not find ${tag} inside ${self}"))

private[ZEnvironment] def getOrElse[A](tag: LightTypeTag, default: => A)(implicit unsafe: Unsafe): A =
self.cache.get(tag) match {
case Some(a) => a.asInstanceOf[A]
case None =>
self.cache.getOrElse(tag, null) match {
case null =>
var index = -1
val iterator = self.map.iterator
var service: A = null.asInstanceOf[A]
Expand All @@ -182,6 +181,7 @@ final class ZEnvironment[+R] private (
self.cache = self.cache.updated(tag, service)
service
}
case a => a.asInstanceOf[A]
}

private[ZEnvironment] def update[A >: R](tag: LightTypeTag, f: A => A)(implicit
Expand Down

0 comments on commit 93be9ea

Please sign in to comment.