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

less allocation in ExecutionPath.add #769

Merged
merged 5 commits into from
Oct 28, 2021
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
30 changes: 29 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sbt.Keys.{
}
import com.typesafe.tools.mima.core.{
DirectMissingMethodProblem,
IncompatibleMethTypeProblem,
IncompatibleResultTypeProblem,
MissingClassProblem,
MissingTypesProblem,
Expand Down Expand Up @@ -112,7 +113,34 @@ ThisBuild / mimaBinaryIssueFilters ++= Seq(
"sangria.parser.ParserConfig.legacyEmptyFields"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.parser.ParserConfig.withLegacyEmptyFields"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.parser.QueryParser.legacyEmptyFields")
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.parser.QueryParser.legacyEmptyFields"),
ProblemFilters.exclude[MissingTypesProblem]("sangria.execution.ExecutionPath"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.execution.ExecutionPath.unapply"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.execution.ExecutionPath.apply"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productElementNames"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("sangria.execution.ExecutionPath.path"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.cacheKeyPath"),
ProblemFilters.exclude[IncompatibleResultTypeProblem]("sangria.execution.ExecutionPath.cacheKey"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.execution.ExecutionPath.copy"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.copy$default$*"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productPrefix"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productArity"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productElement"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productIterator"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.execution.ExecutionPath.canEqual"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"sangria.execution.ExecutionPath.productElementName"),
ProblemFilters.exclude[IncompatibleMethTypeProblem]("sangria.execution.ExecutionPath.this"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.execution.ExecutionPath.this"),
ProblemFilters.exclude[MissingTypesProblem]("sangria.execution.ExecutionPath$")
)

lazy val root = project
Expand Down
49 changes: 30 additions & 19 deletions modules/core/src/main/scala/sangria/execution/ExecutionPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,55 @@ import sangria.marshalling.ResultMarshaller
import sangria.ast
import sangria.schema.ObjectType

case class ExecutionPath private (path: Vector[Any], cacheKeyPath: ExecutionPath.PathCacheKey) {
class ExecutionPath private (
_path: List[Any],
cacheKeyPath: ExecutionPath.PathCacheKey,
pathSizeWithoutIndexes: Int) {
lazy val path: List[Any] = _path.reverse
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could expose a Vector here to have less breaking changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we never use the indexing aspect of the Vector, I vote for getting rid of it if we can have breaking changes.


def add(field: ast.Field, parentType: ObjectType[_, _]) =
new ExecutionPath(path :+ field.outputName, cacheKey :+ field.outputName :+ parentType.name)
new ExecutionPath(
field.outputName :: _path,
parentType.name :: field.outputName :: cacheKey,
pathSizeWithoutIndexes = pathSizeWithoutIndexes + 1)

def withIndex(idx: Int) = new ExecutionPath(path :+ idx, cacheKey)
def withIndex(idx: Int) = new ExecutionPath(idx :: _path, cacheKey, pathSizeWithoutIndexes)

def isEmpty = path.isEmpty
def nonEmpty = path.nonEmpty
def isEmpty = _path.isEmpty
def nonEmpty = _path.nonEmpty

/** @return
* last index in the path, if available
*/
def lastIndex: Option[Int] = path.lastOption.collect { case i: Int => i }
def lastIndex: Option[Int] = _path.headOption.collect { case i: Int => i }

/** @return
* the size of the path excluding the indexes
*/
def size = cacheKeyPath.size / 2
def size = pathSizeWithoutIndexes

def marshal(m: ResultMarshaller): m.Node = m.arrayNode(path.map {
def marshal(m: ResultMarshaller): m.Node = m.arrayNode(_path.reverseIterator.map {
case s: String => m.scalarNode(s, "String", Set.empty)
case i: Int => m.scalarNode(i, "Int", Set.empty)
})
}.toVector)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's faster to reverse a Vector than a List.


def cacheKey: ExecutionPath.PathCacheKey = cacheKeyPath

override def toString = path.foldLeft("") {
case ("", str: String) => str
case (acc, str: String) => acc + "." + str
case (acc, idx: Int) => acc + "[" + idx + "]"

case ("", other) => other.toString
case (acc, other) => acc + "." + other.toString
}
override def toString = _path.reverseIterator
.foldLeft(new StringBuilder) {
case (builder, str: String) =>
if (builder.isEmpty) builder.append(str) else builder.append(".").append(str)
case (builder, idx: Int) => builder.append("[").append(idx).append("]")

case (builder, other) =>
if (builder.isEmpty) builder.append(other.toString())
else builder.append(".").append(other.toString)
}
.result()
}

object ExecutionPath {
type PathCacheKey = Vector[String]
type PathCacheKey = List[String]

val empty = new ExecutionPath(Vector.empty, Vector.empty)
val empty = new ExecutionPath(List.empty, List.empty, pathSizeWithoutIndexes = 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DeprecationTrackerSpec
Executor.execute(schema, query, deprecationTracker = deprecationTracker).await

deprecationTracker.times.get should be(1)
deprecationTracker.ctx.get.path.path should be(Vector("nested", "aa", "bb"))
deprecationTracker.ctx.get.path.path should be(List("nested", "aa", "bb"))
deprecationTracker.ctx.get.field.name should be("deprecated")
deprecationTracker.ctx.get.parentType.name should be("TestType")
}
Expand Down