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#allFieldsUniqueNameAndCondition #2368

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Changes from 2 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: 13 additions & 17 deletions core/src/main/scala/caliban/execution/Field.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,21 @@ case class Field(
private[caliban] val aliasedName: String =
if (alias.isEmpty) name else alias.get

private[caliban] lazy val allFieldsUniqueNameAndCondition: Boolean = {
def inner(fields: List[Field]): Boolean = {
val headCondition = fields.head._condition

val seen = new mutable.HashSet[String]
seen.add(fields.head.aliasedName)

var rem = fields.tail
while (rem ne Nil) {
val f = rem.head
val continue = seen.add(f.aliasedName) && f._condition == headCondition
ghostdogpr marked this conversation as resolved.
Show resolved Hide resolved
if (!continue) return false
// TODO: Change the name to `allConditionsUnique` in the next minor version
private[caliban] def allFieldsUniqueNameAndCondition: Boolean =
fields.isEmpty || fields.tail.isEmpty || allConditionsUniqueLazy

private lazy val allConditionsUniqueLazy: Boolean = {
val headCondition = fields.head._condition
var rem = fields.tail
var res = true
while ((rem ne Nil) && res) {
kyri-petrou marked this conversation as resolved.
Show resolved Hide resolved
val f = rem.head
if (f._condition == headCondition)
rem = rem.tail
}
true
else res = false
}

val fields0 = fields
fields0.isEmpty || fields0.tail.isEmpty || inner(fields0)
res
}

def combine(other: Field): Field =
Expand Down