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

    https://github.com/apache/spark/pull/3288#discussion_r20687461
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/stat/MultivariateOnlineSummarizer.scala
 ---
    @@ -95,22 +93,7 @@ class MultivariateOnlineSummarizer extends 
MultivariateStatisticalSummary with S
         require(n == sample.size, s"Dimensions mismatch when adding new 
sample." +
           s" Expecting $n but got ${sample.size}.")
     
    -    sample match {
    -      case dv: DenseVector => {
    -        var j = 0
    -        while (j < dv.size) {
    -          add(j, dv.values(j))
    -          j += 1
    -        }
    -      }
    -      case sv: SparseVector =>
    -        var j = 0
    -        while (j < sv.indices.size) {
    -          add(sv.indices(j), sv.values(j))
    -          j += 1
    -        }
    -      case v => throw new IllegalArgumentException("Do not support vector 
type " + v.getClass)
    -    }
    +    sample.foreach(true)(x => add(x._1, x._2))
    --- End diff --
    
    This will be faster than 
    ```scala
    sample.foreach(true){
      case (index, value) => add(index, value) 
    }
    ```
    for 5%. 
    
    See the generated bytecode.
    
    With pattern matching.
    ```
       L20
        LINENUMBER 103 L20
        ALOAD 4
        INVOKEVIRTUAL scala/Tuple2._1$mcI$sp ()I
        ISTORE 5
       L21
        ALOAD 4
        INVOKEVIRTUAL scala/Tuple2._2$mcD$sp ()D
        DSTORE 6
       L22
        ALOAD 0
        ILOAD 5
        DLOAD 6
        INVOKEVIRTUAL 
org/apache/spark/mllib/stat/MultivariateOnlineSummarizer.org$apache$spark$mllib$stat$MultivariateOnlineSummarizer$$add
 (ID)V
        GETSTATIC scala/runtime/BoxedUnit.UNIT : Lscala/runtime/BoxedUnit;
        ASTORE 8
    ```
    Without pattern matching.
    ```
       L17
        LINENUMBER 100 L17
        ALOAD 0
        ALOAD 3
        INVOKEVIRTUAL scala/Tuple2._1$mcI$sp ()I
        ALOAD 3
        INVOKEVIRTUAL scala/Tuple2._2$mcD$sp ()D
        INVOKEVIRTUAL 
org/apache/spark/mllib/stat/MultivariateOnlineSummarizer.org$apache$spark$mllib$stat$MultivariateOnlineSummarizer$$add
 (ID)V
    ```


---
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