Github user dbtsai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/3288#discussion_r20615000
  
    --- 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 --
    
    You are right; the `Tuple2[Int, Double]` is specialized, and I mistakenly 
interpreted the bytecode. 
    For the flowing scala code,
    ```scala
    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
       L0
        LINENUMBER 296 L0
        ICONST_0
        ISTORE 2
       L1
        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
       L2
        LINENUMBER 298 L2
        ALOAD 0
        INVOKEVIRTUAL org/apache/spark/mllib/linalg/DenseVector.values ()[D
        ASTORE 4
       L3
        LINENUMBER 299 L3
       FRAME APPEND [I I [D]
        ILOAD 2
        ILOAD 3
        IF_ICMPGE L4
       L5
        LINENUMBER 300 L5
        ALOAD 1
        NEW scala/Tuple2$mcID$sp
        DUP
        ILOAD 2
        ALOAD 4
        ILOAD 2
        DALOAD
        INVOKESPECIAL scala/Tuple2$mcID$sp.<init> (ID)V
        INVOKEINTERFACE scala/Function1.apply 
(Ljava/lang/Object;)Ljava/lang/Object;
        POP
       L6
        LINENUMBER 301 L6
        ILOAD 2
        ICONST_1
        IADD
        ISTORE 2
        GOTO L3
    ```
    
    However, 
    ```
        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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to