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

Optimize field parsing #2102

Merged
merged 2 commits into from
Jan 30, 2024
Merged
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
26 changes: 15 additions & 11 deletions core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import scala.annotation.nowarn

@nowarn("msg=NoWhitespace") // False positive warning in Scala 2.x
private[caliban] trait SelectionParsers extends ValueParsers {

@deprecated("Kept for bincompat only, scheduled to be removed")
def alias(implicit ev: P[Any]): P[String] = P(name ~ ":")
def aliasOrName(implicit ev: P[Any]): P[String] = P(":" ~ name)

def argument(implicit ev: P[Any]): P[(String, InputValue)] = P(name ~ ":" ~ value)
def arguments(implicit ev: P[Any]): P[Map[String, InputValue]] = P("(" ~/ argument.rep ~ ")").map(_.toMap)
Expand All @@ -27,23 +30,24 @@ private[caliban] trait SelectionParsers extends ValueParsers {
def namedType(implicit ev: P[Any]): P[NamedType] = P(name.filter(_ != "null")).map(NamedType(_, nonNull = false))
def listType(implicit ev: P[Any]): P[ListType] = P("[" ~ type_ ~ "]").map(t => ListType(t, nonNull = false))

@deprecated("Kept for bincompat only, scheduled to be removed")
def nonNullType(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!").map {
case t: NamedType => t.copy(nonNull = true)
case t: ListType => t.copy(nonNull = true)
}
def type_(implicit ev: P[Any]): P[Type] = P(nonNullType | namedType | listType)

def type_(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!".!.?).map {
case (t: NamedType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t
case (t: ListType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t
}

def field(implicit ev: P[Any]): P[Field] =
P(Index ~ alias.? ~ name ~ arguments.? ~ directives.? ~ selectionSet.?).map {
case (index, alias, name, args, dirs, sels) =>
Field(
alias,
name,
args.getOrElse(Map()),
dirs.getOrElse(Nil),
sels.getOrElse(Nil),
index
)
P(Index ~ name ~ aliasOrName.? ~ arguments.? ~ directives.? ~ selectionSet.?).map {
case (index, alias, Some(name), args, dirs, sels) =>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the main optimization. If the 3rd parser succeeded (indicated by Some(_)), then it means we have an alias. This is much faster because the 3rd parser (aliasOrName) contains the : assertion at the start of the string rather than the end

Field(Some(alias), name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index)
case (index, name, _, args, dirs, sels) =>
Field(None, name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index)

}

def fragmentName(implicit ev: P[Any]): P[String] = P(name).filter(_ != "on")
Expand Down