-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-7140][MLLIB] only scan the first 16 entries in Vector.hashCode #5697
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,7 @@ sealed trait Vector extends Serializable { | |
|
|
||
| override def equals(other: Any): Boolean = { | ||
| other match { | ||
| case v2: Vector => { | ||
| case v2: Vector => | ||
| if (this.size != v2.size) return false | ||
| (this, v2) match { | ||
| case (s1: SparseVector, s2: SparseVector) => | ||
|
|
@@ -63,20 +63,25 @@ sealed trait Vector extends Serializable { | |
| Vectors.equals(0 until d1.size, d1.values, s1.indices, s1.values) | ||
| case (_, _) => util.Arrays.equals(this.toArray, v2.toArray) | ||
| } | ||
| } | ||
| case _ => false | ||
| } | ||
| } | ||
|
|
||
| override def hashCode(): Int = { | ||
| var result: Int = size + 31 | ||
| var i = 0 | ||
| this.foreachActive { case (index, value) => | ||
|
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. you don't mean to have a case here do you? just a closure not partial function |
||
| // ignore explict 0 for comparison between sparse and dense | ||
| if (value != 0) { | ||
| result = 31 * result + index | ||
| // refer to {@link java.util.Arrays.equals} for hash algorithm | ||
| val bits = java.lang.Double.doubleToLongBits(value) | ||
| result = 31 * result + (bits ^ (bits >>> 32)).toInt | ||
| i += 1 | ||
| // only scan the first 16 nonzeros | ||
| if (i > 16) { | ||
| return result | ||
|
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. note that return in a closure is pretty expensive because it triggers an exception (which requires traversing the current stack). this might be much slower than before. you probably want to pass the max number of things to run into foreachActive.
Contributor
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.
|
||
| } | ||
| } | ||
| } | ||
| result | ||
|
|
||
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.
Why is Vector.hashCode overridden, even though it will never be called (since Sparse & Dense Vector both override it)?
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.
This is just a reference implementation for subclasses to optimize.