From 579a0a68ae6dca08a9a6ef0eb0760bbaa0631a75 Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Mon, 19 Aug 2024 20:44:02 +0300 Subject: [PATCH 1/3] Optimize `Field#allFieldsUniqueNameAndCondition` --- .../main/scala/caliban/execution/Field.scala | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/core/src/main/scala/caliban/execution/Field.scala b/core/src/main/scala/caliban/execution/Field.scala index f8e03d705..68b53a4a0 100644 --- a/core/src/main/scala/caliban/execution/Field.scala +++ b/core/src/main/scala/caliban/execution/Field.scala @@ -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 - 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 || allConditionsUniqueLzy + + private lazy val allConditionsUniqueLzy: Boolean = { + val headCondition = fields.head._condition + var rem = fields.tail + var res = true + 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 = From 7854453b224c8f7f61a09e951f04e2ce4a46ab5b Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Fri, 23 Aug 2024 16:54:43 +0300 Subject: [PATCH 2/3] PR comment --- core/src/main/scala/caliban/execution/Field.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/caliban/execution/Field.scala b/core/src/main/scala/caliban/execution/Field.scala index 68b53a4a0..efa7d64d4 100644 --- a/core/src/main/scala/caliban/execution/Field.scala +++ b/core/src/main/scala/caliban/execution/Field.scala @@ -48,9 +48,9 @@ case class Field( // TODO: Change the name to `allConditionsUnique` in the next minor version private[caliban] def allFieldsUniqueNameAndCondition: Boolean = - fields.isEmpty || fields.tail.isEmpty || allConditionsUniqueLzy + fields.isEmpty || fields.tail.isEmpty || allConditionsUniqueLazy - private lazy val allConditionsUniqueLzy: Boolean = { + private lazy val allConditionsUniqueLazy: Boolean = { val headCondition = fields.head._condition var rem = fields.tail var res = true From 0fb150aa6a3004e4a4dd21221ca973d01d2dad56 Mon Sep 17 00:00:00 2001 From: Kyri Petrou Date: Sat, 24 Aug 2024 11:17:02 +0300 Subject: [PATCH 3/3] Use local `nil` --- core/src/main/scala/caliban/execution/Field.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/caliban/execution/Field.scala b/core/src/main/scala/caliban/execution/Field.scala index efa7d64d4..c2c09284a 100644 --- a/core/src/main/scala/caliban/execution/Field.scala +++ b/core/src/main/scala/caliban/execution/Field.scala @@ -54,7 +54,8 @@ case class Field( val headCondition = fields.head._condition var rem = fields.tail var res = true - while ((rem ne Nil) && res) { + val nil = Nil + while ((rem ne nil) && res) { val f = rem.head if (f._condition == headCondition) rem = rem.tail