-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-6269] [CORE] Use ScalaRunTime's array methods instead of java.lang.reflect.Array in size estimation #4972
Closed
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1fe09de
[SPARK-6269] Use a different implementation of java.lang.reflect.Array
justinuang ca063fc
Fixing a compiler error made while refactoring style
mccheah a557ab8
Using a wrapper around arrays to do casting only once
mccheah 93f4b05
Using Scala instead of Java for the array-reflection implementation.
mccheah 6467759
Including primitive size information inside CastedArray.
mccheah 5d53c4c
Removing unused parameter in visitArray.
mccheah cb67ce2
Fixing a scalastyle error - line too long
mccheah db890ea
Removing CastedArray and just using ScalaRunTime.
mccheah 16ce534
Organizing imports properly
mccheah 18d4b50
Addressing style comments - while loops instead of for loops
mccheah 8527852
Respect CamelCase for numElementsDrawn
mccheah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
core/src/main/scala/org/apache/spark/util/CastedArray.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.util | ||
|
||
/** | ||
* Provides a wrapper around an object that is known to be an array, but the specific | ||
* type for the array is unknown. | ||
* | ||
* Normally, in situations when such an array is to be accessed reflectively, one would use | ||
* {@link java.lang.reflect.Array} using getLength() and get() methods. However, it turns | ||
* out that such methods are ill-performant. | ||
* | ||
* It turns out it is better to just use instanceOf and lots of casting over calling through | ||
* to the native C implementation. There is some discussion and a sample code snippet in | ||
* <a href=https://bugs.openjdk.java.net/browse/JDK-8051447>an open JDK ticket</a>. In this | ||
* class, that approach is implemented in an alternative way: creating a wrapper object to | ||
* wrap the array allows the cast to be done once, so the overhead of casting multiple times | ||
* is also avoided. It turns out we invoke the get() method to get the value of the array | ||
* numerous times, so doing the cast just once is worth the cost of constructing the wrapper | ||
* object for larger arrays. | ||
*/ | ||
sealed trait CastedArray extends Any { | ||
def get(i: Int): AnyRef | ||
def getLength(): Int | ||
} | ||
|
||
object CastedArray { | ||
def castAndWrap(arr: AnyRef): CastedArray = { | ||
if (arr.isInstanceOf[Array[Boolean]]) { | ||
return new BooleanCastedArray(arr.asInstanceOf[Array[Boolean]]) | ||
} else if (arr.isInstanceOf[Array[Byte]]) { | ||
return new ByteCastedArray(arr.asInstanceOf[Array[Byte]]) | ||
} else if (arr.isInstanceOf[Array[Char]]) { | ||
return new CharCastedArray(arr.asInstanceOf[Array[Char]]) | ||
} else if (arr.isInstanceOf[Array[Double]]) { | ||
return new DoubleCastedArray(arr.asInstanceOf[Array[Double]]) | ||
} else if (arr.isInstanceOf[Array[Float]]) { | ||
return new FloatCastedArray(arr.asInstanceOf[Array[Float]]) | ||
} else if (arr.isInstanceOf[Array[Int]]) { | ||
return new IntCastedArray(arr.asInstanceOf[Array[Int]]) | ||
} else if (arr.isInstanceOf[Array[Long]]) { | ||
return new LongCastedArray(arr.asInstanceOf[Array[Long]]) | ||
} else if (arr.isInstanceOf[Array[Object]]) { | ||
return new ObjectCastedArray(arr.asInstanceOf[Array[Object]]) | ||
} else if (arr.isInstanceOf[Array[Short]]) { | ||
return new ShortCastedArray(arr.asInstanceOf[Array[Short]]) | ||
} else { | ||
throw createBadArrayException(arr) | ||
} | ||
} | ||
|
||
// Boxing is not ideal, but we want to return AnyRef here. An alternative implementation | ||
// that used Java wouldn't force explicitly boxing... but returning Object there would | ||
// make the boxing happen implicitly anyways. In practice this tends to be okay | ||
// in terms of performance. | ||
private class BooleanCastedArray(val arr: Array[Boolean]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Boolean.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class ByteCastedArray(val arr: Array[Byte]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Byte.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class CharCastedArray(val arr: Array[Char]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Char.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class DoubleCastedArray(val arr: Array[Double]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Double.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class FloatCastedArray(val arr: Array[Float]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Float.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class IntCastedArray(val arr: Array[Int]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Int.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class LongCastedArray(val arr: Array[Long]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Long.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class ObjectCastedArray(val arr: Array[Object]) extends AnyVal with CastedArray { | ||
override def get(i: Int): Object = arr(i) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private class ShortCastedArray(val arr: Array[Short]) extends AnyVal with CastedArray { | ||
override def get(i: Int): AnyRef = Short.box(arr(i)) | ||
override def getLength(): Int = arr.length | ||
} | ||
|
||
private def createBadArrayException(badArray : Object): RuntimeException = { | ||
if (badArray == null) { | ||
return new NullPointerException("Array argument is null"); | ||
} else if (!badArray.getClass().isArray()) { | ||
return new IllegalArgumentException("Argument is not an array"); | ||
} else { | ||
return new IllegalArgumentException("Array is of incompatible type"); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
package org.apache.spark.util | ||
|
||
import java.lang.management.ManagementFactory | ||
import java.lang.reflect.{Array => JArray} | ||
import java.lang.reflect.Field | ||
import java.lang.reflect.Modifier | ||
import java.util.IdentityHashMap | ||
|
@@ -185,7 +184,8 @@ private[spark] object SizeEstimator extends Logging { | |
private val ARRAY_SAMPLE_SIZE = 100 // should be lower than ARRAY_SIZE_FOR_SAMPLING | ||
|
||
private def visitArray(array: AnyRef, cls: Class[_], state: SearchState) { | ||
val length = JArray.getLength(array) | ||
val castedArray: CastedArray = CastedArray.castAndWrap(array) | ||
val length = castedArray.getLength | ||
val elementClass = cls.getComponentType | ||
|
||
// Arrays have object header and length field which is an integer | ||
|
@@ -200,7 +200,7 @@ private[spark] object SizeEstimator extends Logging { | |
|
||
if (length <= ARRAY_SIZE_FOR_SAMPLING) { | ||
for (i <- 0 until length) { | ||
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. while you are doing this, do you mind change the for loop into a while loop? i.e. var i = 0
while (i < length) {
...
i += 1
} |
||
state.enqueue(JArray.get(array, i)) | ||
state.enqueue(castedArray.get(i)) | ||
} | ||
} else { | ||
// Estimate the size of a large array by sampling elements without replacement. | ||
|
@@ -213,7 +213,7 @@ private[spark] object SizeEstimator extends Logging { | |
index = rand.nextInt(length) | ||
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. the for loop above here too |
||
} while (drawn.contains(index)) | ||
drawn.add(index) | ||
val elem = JArray.get(array, index) | ||
val elem = castedArray.get(index) | ||
size += SizeEstimator.estimate(elem, state.visited) | ||
} | ||
state.size += ((length / (ARRAY_SAMPLE_SIZE * 1.0)) * size).toLong | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Part of the reason why boxing is okay is because SizeEstimator doesn't actually use CastedArray.get() on primitive arrays. If it gets a primitive array, size estimation just multiplies the size of the array by the size of the primitive type. So we don't ever invoke get() in those situations.
Nevertheless, boxing looks ugly and I'm welcome to suggestions on better ways to do this.