-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-4581][MLlib] Refactorize StandardScaler to improve the transformation performance #3435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,7 +85,7 @@ class StandardScalerModel private[mllib] ( | |
| f | ||
| } | ||
|
|
||
| private lazy val shift: Array[Double] = mean.toArray | ||
| private val shift: Array[Double] = mean.toArray | ||
|
|
||
| /** | ||
| * Applies standardization transformation on a vector. | ||
|
|
@@ -97,19 +97,24 @@ class StandardScalerModel private[mllib] ( | |
| override def transform(vector: Vector): Vector = { | ||
| require(mean.size == vector.size) | ||
| if (withMean) { | ||
| // By default, Scala generates Java methods for member variables. So every time when | ||
| // the member variables are accessed, `invokespecial` will be called which is expensive. | ||
| // This can be avoid by having a local reference of `shift`. | ||
| val localShift = shift | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I'll change it back to lazy since it will not be evaluated in those branches which don't use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For different implementation of vector, toArray can be very expensive. For example, toArray for sparse vector requires to create a new array object and loop through all the non zero values. As a result, we can have a global |
||
| vector match { | ||
| case dv: DenseVector => | ||
| val values = dv.values.clone() | ||
| val size = values.size | ||
| var i = 0 | ||
| if (withStd) { | ||
| // Having a local reference of `factor` to avoid overhead as the comment before. | ||
| val localFactor = factor | ||
| var i = 0 | ||
| while (i < size) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we move |
||
| values(i) = (values(i) - localShift(i)) * localFactor(i) | ||
| i += 1 | ||
| } | ||
| } else { | ||
| var i = 0 | ||
| while (i < size) { | ||
| values(i) -= localShift(i) | ||
| i += 1 | ||
|
|
@@ -119,6 +124,7 @@ class StandardScalerModel private[mllib] ( | |
| case v => throw new IllegalArgumentException("Do not support vector type " + v.getClass) | ||
| } | ||
| } else if (withStd) { | ||
| // Having a local reference of `factor` to avoid overhead as the comment before. | ||
| val localFactor = factor | ||
| vector match { | ||
| case dv: DenseVector => | ||
|
|
@@ -135,9 +141,9 @@ class StandardScalerModel private[mllib] ( | |
| // so we can re-use it to save memory. | ||
| val indices = sv.indices | ||
| val values = sv.values.clone() | ||
| val size = values.size | ||
| val nnz = values.size | ||
| var i = 0 | ||
| while (i < size) { | ||
| while (i < nnz) { | ||
| values(i) *= localFactor(indices(i)) | ||
| i += 1 | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is worth to leave a comment here and explain why we need local reference.