spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dbtsai <>
Subject [GitHub] spark pull request: [SPARK-4431][MLlib] Implement efficient active...
Date Wed, 19 Nov 2014 23:00:05 GMT
Github user dbtsai commented on a diff in the pull request:
    --- 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
    +   *                      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 --
    You are right; the `Tuple2[Int, Double]` is specialized, and I mistakenly interpreted
the bytecode. 
    For the flowing scala code,
    def foreach[@specialized(Unit) U](f: ((Int, Double)) => U) {
      var i = 0
      val localValuesSize = values.size
      val localValues = values
      while (i < localValuesSize) {
        f(i, localValues(i))
        i += 1
    the generated bytecode will be
      public foreach(Lscala/Function1;)V
        LINENUMBER 296 L0
        ISTORE 2
        LINENUMBER 297 L1
        GETSTATIC scala/Predef$.MODULE$ : Lscala/Predef$;
        ALOAD 0
        INVOKEVIRTUAL org/apache/spark/mllib/linalg/DenseVector.values ()[D
        INVOKEVIRTUAL scala/Predef$.doubleArrayOps ([D)Lscala/collection/mutable/ArrayOps;
        INVOKEINTERFACE scala/collection/mutable/ArrayOps.size ()I
        ISTORE 3
        LINENUMBER 298 L2
        ALOAD 0
        INVOKEVIRTUAL org/apache/spark/mllib/linalg/DenseVector.values ()[D
        ASTORE 4
        LINENUMBER 299 L3
       FRAME APPEND [I I [D]
        ILOAD 2
        ILOAD 3
        IF_ICMPGE L4
        LINENUMBER 300 L5
        ALOAD 1
        NEW scala/Tuple2$mcID$sp
        ILOAD 2
        ALOAD 4
        ILOAD 2
        INVOKESPECIAL scala/Tuple2$mcID$sp.<init> (ID)V
        INVOKEINTERFACE scala/Function1.apply (Ljava/lang/Object;)Ljava/lang/Object;
        LINENUMBER 301 L6
        ILOAD 2
        ISTORE 2
        GOTO L3
        INVOKESPECIAL scala/Tuple2$mcID$sp.<init> (ID)V
        INVOKEINTERFACE scala/Function1.apply (Ljava/lang/Object;)Ljava/lang/Object;
    is expensive, so that's why checking zero in the anonymous function will slow down the
whole thing. 
    I agree with you, the iterator is slow by nature, and we are only interested in foreach
implementation. I'll remove the iterator, and just have foreach method in vector.

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 or file a JIRA ticket
with INFRA.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message