Skip to content

Commit 1ebad60

Browse files
committed
only scan the first 16 nonzeros in Vector.hashCode
1 parent 8509519 commit 1ebad60

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ sealed trait Vector extends Serializable {
5252

5353
override def equals(other: Any): Boolean = {
5454
other match {
55-
case v2: Vector => {
55+
case v2: Vector =>
5656
if (this.size != v2.size) return false
5757
(this, v2) match {
5858
case (s1: SparseVector, s2: SparseVector) =>
@@ -63,20 +63,25 @@ sealed trait Vector extends Serializable {
6363
Vectors.equals(0 until d1.size, d1.values, s1.indices, s1.values)
6464
case (_, _) => util.Arrays.equals(this.toArray, v2.toArray)
6565
}
66-
}
6766
case _ => false
6867
}
6968
}
7069

7170
override def hashCode(): Int = {
7271
var result: Int = size + 31
72+
var i = 0
7373
this.foreachActive { case (index, value) =>
7474
// ignore explict 0 for comparison between sparse and dense
7575
if (value != 0) {
7676
result = 31 * result + index
7777
// refer to {@link java.util.Arrays.equals} for hash algorithm
7878
val bits = java.lang.Double.doubleToLongBits(value)
7979
result = 31 * result + (bits ^ (bits >>> 32)).toInt
80+
i += 1
81+
// only scan the first 16 nonzeros
82+
if (i > 16) {
83+
return result
84+
}
8085
}
8186
}
8287
result

0 commit comments

Comments
 (0)