Skip to content

Commit

Permalink
Merge pull request #775 from sangria-graphql/introduce_cacheKeyReversed
Browse files Browse the repository at this point in the history
use 'cacheKeyReversed' to avoid creating vectors
  • Loading branch information
yanns authored Oct 29, 2021
2 parents 3deafc1 + 47b5a4e commit d9bd889
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d9bd889

Please sign in to comment.