kunwp1 commented on code in PR #9515:
URL: https://github.com/apache/texera/pull/9515#discussion_r4596588831


##########
common/workflow-core/src/main/scala/org/apache/texera/amber/core/tuple/Tuple.scala:
##########
@@ -83,25 +83,33 @@ case class Tuple @JsonCreator() (
     this
   }
 
-  override def hashCode: Int = 
util.Arrays.deepHashCode(getFields.map(_.asInstanceOf[AnyRef]))
+  override def hashCode: Int = 
util.Arrays.deepHashCode(getFields.asInstanceOf[Array[AnyRef]])
 
   override def equals(obj: Any): Boolean =
     obj match {
-      case that: Tuple =>
-        this.schema == that.schema &&
-          this.getFields.zip(that.getFields).forall {
-            case (field1: Array[Byte], field2: Array[Byte]) =>
-              field1.sameElements(field2) // for Binary, use sameElements 
instead of == to compare
-            case (field1, field2) =>
-              field1 == field2
-          }
+      case that: Tuple if this.schema == that.schema =>
+        val left = this.getFields
+        val right = that.getFields
+        (0 until math.min(left.length, right.length)).forall(i => 
fieldEquals(left(i), right(i)))
       case _ => false
     }
 
+  private def fieldEquals(left: Any, right: Any): Boolean =
+    (left, right) match {
+      case (l: Array[Byte], r: Array[Byte]) => util.Arrays.equals(l, r)
+      case _                                => left == right
+    }

Review Comment:
   My snippet here from last round wasn't as cheap as it looked, sorry. `(left, 
right) match` with a `case _` still compiles to `new scala/Tuple2` on every 
`fieldEquals` call, and `(0 until n).forall` builds a `Range` and a capturing 
lambda on every `equals`. I checked with `javap` on the compiled class. 
Distinct and the set operators call this per row, so an index loop with plain 
type tests avoids all three:
   
   ```scala
           val left = this.getFields
           val right = that.getFields
           val fieldCount = math.min(left.length, right.length)
           var index = 0
           while (index < fieldCount && fieldEquals(left(index), right(index))) 
index += 1
           index == fieldCount
         case _ => false
       }
   
     private def fieldEquals(left: Any, right: Any): Boolean =
       if (left.isInstanceOf[Array[Byte]] && right.isInstanceOf[Array[Byte]])
         util.Arrays.equals(left.asInstanceOf[Array[Byte]], 
right.asInstanceOf[Array[Byte]])
       else left == right
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to