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

Micro-optimize usages of Configurator #2250

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -26,7 +26,7 @@ final private class QuickRequestHandler[R](

def configure(config: ExecutionConfiguration)(implicit trace: Trace): QuickRequestHandler[R] =
new QuickRequestHandler[R](
interpreter.wrapExecutionWith[R, Any](Configurator.setWith(config)(_)),
interpreter.wrapExecutionWith[R, Any](Configurator.ref.locally(config)(_)),
wsConfig
)

Expand Down
2 changes: 1 addition & 1 deletion adapters/quick/src/main/scala/caliban/quick/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ package object quick {
val run: RIO[R, Nothing] =
QuickAdapter(interpreter)
.runServer(port, apiPath, graphiqlPath, uploadPath)
.provideSomeLayer[R](ZLayer.scoped[Any](Configurator.set(executionConfig)))
.provideSomeLayer[R](ZLayer.scoped[Any](Configurator.ref.locallyScoped(executionConfig)))

ZIOApp.fromZIO(run.asInstanceOf[RIO[Any, Nothing]]).main(Array.empty)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class NestedZQueryBenchmark {
def simpleParallelQuery100(): Any = {
val io =
simple100
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -98,7 +98,7 @@ class NestedZQueryBenchmark {
def simpleParallelQuery1000(): Any = {
val io =
simple1000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -107,7 +107,7 @@ class NestedZQueryBenchmark {
def simpleParallelQuery10000(): Any = {
val io =
simple10000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -116,7 +116,7 @@ class NestedZQueryBenchmark {
def simpleSequentialQuery100(): Any = {
val io =
simple100
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -125,7 +125,7 @@ class NestedZQueryBenchmark {
def simpleSequentialQuery1000(): Any = {
val io =
simple1000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -134,7 +134,7 @@ class NestedZQueryBenchmark {
def simpleSequentialQuery10000(): Any = {
val io =
simple10000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -143,7 +143,7 @@ class NestedZQueryBenchmark {
def simpleBatchedQuery100(): Any = {
val io =
simple100
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -152,7 +152,7 @@ class NestedZQueryBenchmark {
def simpleBatchedQuery1000(): Any = {
val io =
simple1000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(simpleQuery)
run(io)
}
Expand All @@ -161,95 +161,95 @@ class NestedZQueryBenchmark {
def simpleBatchedQuery10000(): Any = {
val io =
simple10000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(simpleQuery)
run(io)
}

@Benchmark
def multifieldParallelQuery100(): Any = {
val io = multifield100
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldParallelQuery1000(): Any = {
val io = multifield1000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldParallelQuery10000(): Any = {
val io = multifield10000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldSequentialQuery100(): Any = {
val io = multifield100
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldSequentialQuery1000(): Any = {
val io = multifield1000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldSequentialQuery10000(): Any = {
val io = multifield10000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldBatchedQuery100(): Any = {
val io = multifield100
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldBatchedQuery1000(): Any = {
val io = multifield1000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def multifieldBatchedQuery10000(): Any = {
val io = multifield10000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def deepParallelQuery100(): Any = {
val io = deep100
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(deepQuery)
run(io)
}

@Benchmark
def deepParallelQuery1000(): Any = {
val io = deep1000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(deepQuery)
run(io)
}
Expand All @@ -258,7 +258,7 @@ class NestedZQueryBenchmark {
def deepParallelQuery10000(): Any = {
val io =
deep10000
.wrapExecutionWith(Configurator.setWith(parallel)(_))
.wrapExecutionWith(Configurator.ref.locally(parallel)(_))
.execute(deepQuery)
run(io)
}
Expand All @@ -267,7 +267,7 @@ class NestedZQueryBenchmark {
def deepSequentialQuery100(): Any = {
val io =
deep100
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(deepQuery)
run(io)
}
Expand All @@ -276,7 +276,7 @@ class NestedZQueryBenchmark {
def deepSequentialQuery1000(): Any = {
val io =
deep1000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(deepQuery)
run(io)
}
Expand All @@ -285,55 +285,55 @@ class NestedZQueryBenchmark {
def deepSequentialQuery10000(): Any = {
val io =
deep10000
.wrapExecutionWith(Configurator.setWith(sequential)(_))
.wrapExecutionWith(Configurator.ref.locally(sequential)(_))
.execute(deepQuery)
run(io)
}

@Benchmark
def deepBatchedQuery100(): Any = {
val io = deep100
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(deepQuery)
run(io)
}

@Benchmark
def deepBatchedQuery1000(): Any = {
val io = deep1000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(deepQuery)
run(io)
}

@Benchmark
def deepBatchedQuery10000(): Any = {
val io = deep10000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(deepQuery)
run(io)
}

@Benchmark
def noWrappersBenchmark(): Any = {
val io = multifield1000
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def apolloTracingBenchmark(): Any = {
val io = apolloInterpreter
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}

@Benchmark
def metricsBenchmark(): Any = {
val io = metricsInterpreter
.wrapExecutionWith(Configurator.setWith(batched)(_))
.wrapExecutionWith(Configurator.ref.locally(batched)(_))
.execute(multifieldQuery)
run(io)
}
Expand Down
35 changes: 10 additions & 25 deletions core/src/main/scala/caliban/Configurator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,48 @@ object Configurator {
/**
* Creates a ZLayer that can be used to set this configuration at the application level
*/
def toLayer: ULayer[Unit] = ZLayer.scoped(set(self))
def toLayer: ULayer[Unit] = ZLayer.scoped(ref.locallyScoped(self))
}

private val configRef: FiberRef[ExecutionConfiguration] =
private[caliban] val ref: FiberRef[ExecutionConfiguration] =
Unsafe.unsafe(implicit u => FiberRef.unsafe.make(ExecutionConfiguration()))

private[caliban] val configuration: UIO[ExecutionConfiguration] =
configRef.get

private[caliban] def locallyWith[R, E, A](
cfg: ExecutionConfiguration => ExecutionConfiguration
)(
f: ZIO[R, E, A]
): ZIO[R, E, A] =
configRef.locallyWith(cfg)(f)

private[caliban] def set(cfg: ExecutionConfiguration): URIO[Scope, Unit] =
configRef.locallyScoped(cfg)

private[caliban] def setWith[R, E, A](cfg: ExecutionConfiguration)(f: ZIO[R, E, A])(implicit
trace: Trace
): ZIO[R, E, A] =
configRef.locally(cfg)(f)
private[caliban] val skipValidation: UIO[Boolean] =
ref.getWith(cfg => ZIO.succeed(cfg.skipValidation))

/**
* Skip validation of the query.
* @param skip if true, the query will not be validated (in that case, the `validations` field is ignored).
*/
def setSkipValidation(skip: Boolean): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(skipValidation = skip))
ref.locallyScopedWith(_.copy(skipValidation = skip))

/**
* Set the validations to run on the query during the validation phase.
* @param validations the validations to run on the query during the validation phase.
*/
def setValidations(validations: List[QueryValidation]): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(validations = validations))
ref.locallyScopedWith(_.copy(validations = validations))

/**
* Enable or disable introspection queries.
* @param enable if true, introspection queries are allowed.
*/
def setEnableIntrospection(enable: Boolean): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(enableIntrospection = enable))
ref.locallyScopedWith(_.copy(enableIntrospection = enable))

/**
* Set the execution strategy to use (sequential, parallel, batched).
* @param queryExecution the execution strategy to use.
*/
def setQueryExecution(queryExecution: QueryExecution): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(queryExecution = queryExecution))
ref.locallyScopedWith(_.copy(queryExecution = queryExecution))

/**
* Enable or disable mutations for GET requests. See [[ExecutionConfiguration]] for more details
*/
def setAllowMutationsOverGetRequests(allow: Boolean): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(allowMutationsOverGetRequests = allow))
ref.locallyScopedWith(_.copy(allowMutationsOverGetRequests = allow))

/**
* Sets an effect which will be used to create a new ZQuery [[zio.query.Cache]] for each query execution.
Expand All @@ -97,5 +82,5 @@ object Configurator {
* @see [[ExecutionConfiguration]] for more details
*/
def setQueryCache(mkCache: UIO[Cache]): URIO[Scope, Unit] =
configRef.locallyScopedWith(_.copy(queryCache = mkCache))
ref.locallyScopedWith(_.copy(queryCache = mkCache))
}
Loading
Loading