Skip to content

Commit

Permalink
Optimize Field#allFieldsUniqueNameAndCondition (#2368)
Browse files Browse the repository at this point in the history
* Optimize `Field#allFieldsUniqueNameAndCondition`

* PR comment

* Use local `nil`
  • Loading branch information
kyri-petrou authored Aug 24, 2024
1 parent 2f175a0 commit 9929056
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 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,22 @@ 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
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
val nil = Nil
while ((rem ne nil) && res) {
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

0 comments on commit 9929056

Please sign in to comment.