Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/3288#discussion_r20554090
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
---
@@ -76,6 +76,22 @@ sealed trait Vector extends Serializable {
def copy: Vector = {
throw new NotImplementedError(s"copy is not implemented for
${this.getClass}.")
}
+
+ /**
+ * It will return the iterator for the active elements of dense and
sparse vector as
+ * (index, value) pair. Note that foreach method can be overridden for
better performance
+ * in different vector implementation.
+ *
+ * @param skippingZeros Skipping zero elements explicitly if true. It
will be useful when we
+ * iterator through dense vector having lots of
zero elements which
+ * we want to skip. Default is false.
+ * @return Iterator[(Int, Double)] where the first element in the tuple
is the index,
+ * and the second element is the corresponding value.
+ */
+ private[spark] def activeIterator(skippingZeros: Boolean):
Iterator[(Int, Double)]
--- End diff --
Okay, the issue is in the anonymous function. Basically, scala will convert
primitive index: Int and value: Double into boxed object in order to have them
in tuple. In my testing dataset, there are so many zeros explicitly, and even
those values with zero have to be converted to tuple before we do the `if
statement`. That's why it's dramatically faster if we do the `if statement`
before calling the anonymous function.
Changing the signature of `foreach` into
def foreach[@specialized(Unit) U](f: (Int, Double) => U)
to take two primitive variables will solve this problem, but it will not
comply the interface of `foreach`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]