From 47b5a4ea03b16123f75dc0ee071620ad838877e1 Mon Sep 17 00:00:00 2001 From: Yann Simon Date: Fri, 29 Oct 2021 09:26:59 +0200 Subject: [PATCH] use 'cacheKeyReversed' to avoid creating vectors --- .../src/main/scala/sangria/execution/ExecutionPath.scala | 2 ++ .../src/main/scala/sangria/execution/FieldCollector.scala | 5 +++-- .../src/main/scala/sangria/execution/ValueCollector.scala | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/scala/sangria/execution/ExecutionPath.scala b/modules/core/src/main/scala/sangria/execution/ExecutionPath.scala index 3b2950e2..3505c5d1 100644 --- a/modules/core/src/main/scala/sangria/execution/ExecutionPath.scala +++ b/modules/core/src/main/scala/sangria/execution/ExecutionPath.scala @@ -38,6 +38,7 @@ class ExecutionPath private ( }.toVector) def cacheKey: ExecutionPath.PathCacheKey = cacheKeyPath.reverseIterator.toVector + def cacheKeyReversed: ExecutionPath.PathCacheKeyReversed = cacheKeyPath override def toString: String = _path.reverseIterator .foldLeft(new StringBuilder) { @@ -54,6 +55,7 @@ class ExecutionPath private ( object ExecutionPath { type PathCacheKey = Vector[String] + type PathCacheKeyReversed = List[String] val empty = new ExecutionPath(List.empty, List.empty, pathSizeWithoutIndexes = 0) } diff --git a/modules/core/src/main/scala/sangria/execution/FieldCollector.scala b/modules/core/src/main/scala/sangria/execution/FieldCollector.scala index b9a9dd00..f9d50c7b 100644 --- a/modules/core/src/main/scala/sangria/execution/FieldCollector.scala +++ b/modules/core/src/main/scala/sangria/execution/FieldCollector.scala @@ -17,14 +17,15 @@ class FieldCollector[Ctx, Val]( valueCollector: ValueCollector[Ctx, _], exceptionHandler: ExceptionHandler) { - private val resultCache = Cache.empty[(ExecutionPath.PathCacheKey, String), Try[CollectedFields]] + private val resultCache = + Cache.empty[(ExecutionPath.PathCacheKeyReversed, String), Try[CollectedFields]] def collectFields( path: ExecutionPath, tpe: ObjectType[Ctx, _], selections: Vector[ast.SelectionContainer]): Try[CollectedFields] = resultCache.getOrElseUpdate( - path.cacheKey -> tpe.name, { + path.cacheKeyReversed -> tpe.name, { val builder: Try[CollectedFieldsBuilder] = Success(new CollectedFieldsBuilder) selections.foldLeft(builder) { case (acc, s) => diff --git a/modules/core/src/main/scala/sangria/execution/ValueCollector.scala b/modules/core/src/main/scala/sangria/execution/ValueCollector.scala index 990bed77..233c897b 100644 --- a/modules/core/src/main/scala/sangria/execution/ValueCollector.scala +++ b/modules/core/src/main/scala/sangria/execution/ValueCollector.scala @@ -24,7 +24,7 @@ class ValueCollector[Ctx, Input]( new ValueCoercionHelper[Ctx](sourceMapper, deprecationTracker, Some(userContext)) private val argumentCache = - Cache.empty[(ExecutionPath.PathCacheKey, Vector[ast.Argument]), Try[Args]] + Cache.empty[(ExecutionPath.PathCacheKeyReversed, Vector[ast.Argument]), Try[Args]] def getVariableValues( definitions: Vector[ast.VariableDefinition], @@ -83,7 +83,7 @@ class ValueCollector[Ctx, Input]( ValueCollector.emptyArgs else argumentCache.getOrElseUpdate( - path.cacheKey -> argumentAsts, + path.cacheKeyReversed -> argumentAsts, getArgumentValues(forAstNode, argumentDefs, argumentAsts, variables)) def getArgumentValues(