Skip to content
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

Unify ZQuery.foreach implementations and cleanups #469

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class FromRequestBenchmark {
unsafeRunCache(query, Cache.unsafeMake(count))
}

@Benchmark
def fromRequestUncached(): Long = {
val reqs = Chunk.fromIterable((0 until count).map(i => ZQuery.fromRequest(Req(i))(ds)))
val query = ZQuery.collectAllBatched(reqs).map(_.sum.toLong)
unsafeRun(query.uncached)
}

@Benchmark
def fromRequestZipRight(): Long = {
val reqs = Chunk.fromIterable((0 until count).map(i => ZQuery.fromRequest(Req(i))(ds)))
Expand Down
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ lazy val zioQuery = crossProject(JSPlatform, JVMPlatform)
.settings(enableZIO())
.settings(scalacOptions += "-Wconf:msg=[zio.stacktracer.TracingImplicits.disableAutoTrace]:silent")
.settings(
libraryDependencies ++= Seq(
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0"
),
scalacOptions ++=
(if (scalaBinaryVersion.value == "3")
Seq()
Expand Down
24 changes: 6 additions & 18 deletions zio-query/shared/src/main/scala/zio/query/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,21 @@ object Cache {
ev: A <:< Request[E, B],
trace: Trace
): UIO[Either[Promise[E, B], Promise[E, B]]] =
ZIO.fiberId.map(lookupUnsafe(request, _)(Unsafe.unsafe, implicitly))

def lookupUnsafe[E, A, B](
request: A,
fiberId: FiberId
)(implicit
unsafe: Unsafe,
ev: A <:< Request[E, B]
ZIO.fiberId.map(lookupUnsafe(request, _)(Unsafe.unsafe))

def lookupUnsafe[E, A, B](request: Request[_, _], fiberId: FiberId)(implicit
unsafe: Unsafe
): Either[Promise[E, B], Promise[E, B]] = {
val newPromise = Promise.unsafe.make[E, B](fiberId)
val existing = map.putIfAbsent(request, newPromise).asInstanceOf[Promise[E, B]]
if (existing eq null) Left(newPromise) else Right(existing)
}

def put[E, A](request: Request[E, A], result: Promise[E, A])(implicit trace: Trace): UIO[Unit] =
ZIO.succeed(putUnsafe(request, result))

def putUnsafe[E, A](request: Request[E, A], result: Promise[E, A]): Unit = {
map.put(request, result)
()
}
ZIO.succeed(map.put(request, result))

def remove[E, A](request: Request[E, A])(implicit trace: Trace): UIO[Unit] =
ZIO.succeed {
map.remove(request)
()
}
ZIO.succeed(map.remove(request))
}

// TODO: Initialize the map with a sensible default value. Default is 16, which seems way too small for a cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import zio.stacktracer.TracingImplicits.disableAutoTrace

import scala.collection.immutable.HashMap
import scala.collection.mutable
import scala.collection.compat._

/**
* A `CompletedRequestMap` is a universally quantified mapping from requests of
Expand Down Expand Up @@ -76,11 +77,8 @@ final class CompletedRequestMap private (private val map: HashMap[Any, Exit[Any,
def isEmpty: Boolean =
map.isEmpty

private[query] def toMutableMap: mutable.HashMap[Request[?, ?], Exit[Any, Any]] = {
val map0 = new mutable.HashMap[Request[?, ?], Exit[Any, Any]]()
map0.sizeHint(map.size)
map0 ++= map.asInstanceOf[HashMap[Request[?, ?], Exit[Any, Any]]]
}
private[query] def toMutableMap: mutable.HashMap[Request[?, ?], Exit[Any, Any]] =
mutable.HashMap.from(map.asInstanceOf[HashMap[Request[?, ?], Exit[Any, Any]]])

override def toString: String =
s"CompletedRequestMap(${map.mkString(", ")})"
Expand All @@ -97,11 +95,8 @@ object CompletedRequestMap {
/**
* Constructs a completed requests map from the specified results.
*/
def fromIterable[E, A](iterable: Iterable[(Request[E, A], Exit[E, A])]): CompletedRequestMap = {
val builder = HashMap.newBuilder[Any, Exit[Any, Any]]
builder ++= iterable
new CompletedRequestMap(builder.result())
}
def fromIterable[E, A](iterable: Iterable[(Request[E, A], Exit[E, A])]): CompletedRequestMap =
new CompletedRequestMap(HashMap.from(iterable))

/**
* Constructs a completed requests map from the specified optional results.
Expand Down
Loading
Loading